Skip to contentSkip to conversation

netfilter: nf_queue: limit the hook drop flush to the unregistered hook point

PATCH In review Antonio Ojea 3 participants 8 messages Sep 28 – Oct 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 netfilter: nf_queue: limit the hook drop flush to the unregistered hook point

Unregistering a netfilter hook calls nf_queue_nf_hook_drop(), which makes nfqnl_nf_hook_drop() walk every nfqueue instance of the network namespace and flush it, reinjecting every packet that waits for a verdict with NF_DROP. Deleting an nftables base chain of any table, family and hook point therefore drops the packets queued by hooks of unrelated tables, families and hook points. The verdict that userspace sends for them afterwards fails with -ENOENT.

Before commit 039b40ee5854 ("netfilter: nf_queue: only call synchronize_net twice if nf_queue is active") nf_queue_nf_hook_drop() received the removed hook and nfqueue only dropped the entries queued by that hook. The commit removed the argument and the comparison because base chain removal while nfqueue is in use was rare at the time. With base chains added and removed by userspace agents while another program holds packets in a queue this is no longer the case. Commit 960632ece694 ("netfilter: convert hook list to an array") then replaced the hook pointer in struct nf_queue_entry with an index into the hook array of the hook point, so a comparison per hook is no longer possible. A comparison per hook point is: the index of an entry is only stale when the array of its own hook point changed.

Pass the nf_hook_ops being unregistered down to the queue handler and add nf_hook_cmp() to nfnetlink_queue, a cmpfn for nfqnl_flush() that matches the entries whose state.pf and state.hook are the hook point of that hook. The handler takes the nf_hook_ops rather than the family of the modified array to keep a single argument with NULL meaning "flush everything", which nf_ct_iterate_destroy() uses on module exit and __nft_ct_set_destroy() uses when a ct zone expression goes away and a queued packet may still reference its conntrack template. The cmpfn resolves the family itself. An NFPROTO_INET hook is registered in the IPv4 and the IPv6 array, so it matches the entries of both families; nf_unregister_net_hook() calls __nf_unregister_net_hook() for the two arrays back to back and the second flush finds nothing left. If the shrink of one array fails to allocate, that array keeps its dummy hook and its entries stay valid, so the flush for the other family drops them without need; no entry with a stale index survives. An NFPROTO_INET hook at NF_INET_INGRESS is registered in the netdev ingress array, the same mapping that nf_static_key_inc() applies. nfqueue does not accept the ingress and egress hooks (nft_queue_validate()), the mapping keeps the comparison consistent with the registration.

The entries queued from the hook point whose array changed are still dropped, their hook_index is stale. Registering a hook in front of the one that queued a packet still makes nf_reinject() resume at a stale index, so the packet traverses the queueing hook a second time; this is not changed here.

Tested with the reproducer script from the thread, unchanged: it queues 20 UDP packets from an inet output chain to a queue program that delays every verdict by 100 ms and changes the ruleset while they wait. Before this change deleting an unrelated ip6 base chain or an unrelated ip6 table delivers 3 of 20 packets and the queue program exits on ENOENT; with it all 20 are delivered. Deleting a base chain at the same hook point (inet output, another priority, in the same or another table) or an ip table with an output base chain still drops the queued packets; deleting an ip6 table with an output base chain or a netdev ingress chain does not. Adding a base chain in front of the queueing one still queues the packets twice. The nft_queue.sh selftest passes, with CONFIG_NETFILTER_NETLINK_QUEUE=y and built as a module. The following patch adds a test for both outcomes that holds the verdicts instead of delaying them.

The code and this commit message were written with an LLM coding assistant from a task description that contained the report, the suggestion from the thread and the two commits above. The author reviewed the result, directed the changes and validated them with the reproducer and the selftest in virtme-ng before and after the change.

---
v1: https://lore.kernel.org/netfilter-devel/[email protected]/

Changes in v2:
- rebase on nf-next. Convert the nf_queue_nf_hook_drop() call that commit 36eae0956f65 ("netfilter: nft_ct: drop pending enqueued packets on removal") added to __nft_ct_set_destroy(); it passes NULL and keeps flushing every queued packet. Mention this caller in the commit message and in the comment on the NULL case.
https://lore.kernel.org/netfilter-devel/[email protected]/

 include/net/netfilter/nf_queue.h  |  3 +-
 net/netfilter/core.c              |  2 +-
 net/netfilter/nf_conntrack_core.c |  2 +-
 net/netfilter/nf_internals.h      |  2 +-
 net/netfilter/nf_queue.c          |  4 +--
 net/netfilter/nfnetlink_queue.c   | 49 +++++++++++++++++++++++++++++--
 net/netfilter/nft_ct.c            |  2 +-
 7 files changed, 55 insertions(+), 9 deletions(-)
include/net/netfilter/nf_queue.h +2 -1
index fc3e81c07364..86021c55ba3c 100644
@@ -35,7 +35,8 @@ struct nf_queue_entry {
 struct nf_queue_handler {
 	int		(*outfn)(struct nf_queue_entry *entry,
 				 unsigned int queuenum);
-	void		(*nf_hook_drop)(struct net *net);
+	void		(*nf_hook_drop)(struct net *net,
+					const struct nf_hook_ops *ops);
 };
 
 void nf_register_queue_handler(const struct nf_queue_handler *qh);
net/netfilter/core.c +1 -1
index 675a1034b340..9351b480115d 100644
@@ -515,7 +515,7 @@ static void __nf_unregister_net_hook(struct net *net, int pf,
 	if (!p)
 		return;
 
-	nf_queue_nf_hook_drop(net);
+	nf_queue_nf_hook_drop(net, reg);
 	nf_hook_entries_free(p);
 }
 
net/netfilter/nf_conntrack_core.c +1 -1
index d0d9e5ea84a0..37dbd89ac21e 100644
@@ -2411,7 +2411,7 @@ nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data), void *data)
 
 		if (atomic_read(&cnet->count) == 0)
 			continue;
-		nf_queue_nf_hook_drop(net);
+		nf_queue_nf_hook_drop(net, NULL);
 	}
 	up_read(&net_rwsem);
 
net/netfilter/nf_internals.h +1 -1
index 25403023060b..ce5aff2cae71 100644
@@ -24,7 +24,7 @@
 #define CTA_FILTER_FLAG(ctattr) CTA_FILTER_F_ ## ctattr
 
 /* nf_queue.c */
-void nf_queue_nf_hook_drop(struct net *net);
+void nf_queue_nf_hook_drop(struct net *net, const struct nf_hook_ops *ops);
 
 /* nf_log.c */
 int __init netfilter_log_init(void);
net/netfilter/nf_queue.c +2 -2
index 73363ceedebe..166d05e994ac 100644
@@ -129,14 +129,14 @@ bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)
 }
 EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
 
-void nf_queue_nf_hook_drop(struct net *net)
+void nf_queue_nf_hook_drop(struct net *net, const struct nf_hook_ops *ops)
 {
 	const struct nf_queue_handler *qh;
 
 	rcu_read_lock();
 	qh = rcu_dereference(nf_queue_handler);
 	if (qh)
-		qh->nf_hook_drop(net);
+		qh->nf_hook_drop(net, ops);
 	rcu_read_unlock();
 }
 EXPORT_SYMBOL_GPL(nf_queue_nf_hook_drop);
net/netfilter/nfnetlink_queue.c +47 -2

68 lines not on this page · raw message

net/netfilter/nft_ct.c +1 -1
index 3c4c2faa7398..5a8db5592036 100644
@@ -542,7 +542,7 @@ static void __nft_ct_set_destroy(const struct nft_ctx *ctx, struct nft_ct *priv)
 #endif
 #ifdef CONFIG_NF_CONNTRACK_ZONES
 	case NFT_CT_ZONE:
-		nf_queue_nf_hook_drop(ctx->net);
+		nf_queue_nf_hook_drop(ctx->net, NULL);
 		mutex_lock(&nft_ct_pcpu_mutex);
 		if (--nft_ct_pcpu_template_refcnt == 0)
 			nft_ct_tmpl_put_pcpu();

base-commit: 87b80c2f6b05cad9f0ff9136709c62a0f59923e3

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#1 lore raw

PATCH v2 2/2 selftests: netfilter: nft_queue: check the scope of the hook drop flush

Add test_hook_drop_scope. It queues 20 UDP packets from an inet output chain to a queue program that holds all verdicts, deletes a base chain while they wait, and reads the queue length from /proc/net/netfilter/nfnetlink_queue right after the deletion. It then releases the queue program and checks with a counter in the input chain and the packet total of the queue program that the surviving packets are delivered.

Deleting a base chain at another hook point (ip6 prerouting) must keep the 20 queued packets. Deleting a base chain at the queueing hook point (inet output, another priority) drops them, because the index of a queued packet into the hook array of its hook point is stale once that array changed. The second case documents the remaining behaviour so that a change to it is visible.

To hold the packets without depending on timing, add the -H option to the nf_queue helper: it blocks SIGUSR1, reads the first packet and waits in sigwait() until it receives SIGUSR1, then sends the verdicts as usual. The test sends the signal after the ruleset change, so the queue length it reads is exact on slow machines too.

Without the previous patch the first case fails: 0 of 20 packets remain queued after the deletion and none is delivered.

The test and the helper option were written with an LLM coding assistant from a task description; the author reviewed the result, and validated the test in virtme-ng on kernels with and without the previous patch.

Assisted-by LLM

---
v1: https://lore.kernel.org/netfilter-devel/[email protected]/

Changes in v2:
- rebase on nf-next; nf_queue.c gained the -o, -O and -b options, -H is added next to them.

 .../selftests/net/netfilter/nf_queue.c        |  22 +++-
 .../selftests/net/netfilter/nft_queue.sh      | 108 ++++++++++++++++++
 2 files changed, 129 insertions(+), 1 deletion(-)
tools/testing/selftests/net/netfilter/nf_queue.c +21 -1
index 8bbec37f5356..cca6c3cfa9e4 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <errno.h>
+#include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdint.h>
@@ -21,6 +22,7 @@ struct options {
 	bool failopen;
 	bool out_of_order;
 	bool bogus_verdict;
+	bool hold;
 	int verbose;
 	unsigned int queue_num;
 	unsigned int timeout;
@@ -34,6 +36,7 @@ static struct options opts;
 static void help(const char *p)
 {
 	printf("Usage: %s [-c|-v [-vv] ] [-o] [-O] [-b] [-t timeout] [-q queue_num] [-Qdst_queue ] [ -d ms_delay ] [-G]\n", p);
+	printf("          [-H] (hold verdicts until SIGUSR1 is received)\n");
 }
 
 static int parse_attr_cb(const struct nlattr *attr, void *data)
@@ -280,6 +283,7 @@ static int mainloop(void)
 	uint32_t ooo_ids[16];
 	unsigned int portid;
 	int ooo_count = 0;
+	sigset_t hold_set;
 	char *buf;
 	int ret;
 
@@ -289,6 +293,12 @@ static int mainloop(void)
 		exit(EXIT_FAILURE);
 	}
 
+	/* -H: keep SIGUSR1 pending until the first packet is read */
+	sigemptyset(&hold_set);
+	sigaddset(&hold_set, SIGUSR1);
+	if (opts.hold)
+		sigprocmask(SIG_BLOCK, &hold_set, NULL);
+
 	nl = open_queue();
 	portid = mnl_socket_get_portid(nl);
 
@@ -320,6 +330,13 @@ static int mainloop(void)
 		}
 
 		id = ret - MNL_CB_OK;
+		if (opts.hold) {
+			int sig;
+
+			sigwait(&hold_set, &sig);
+			opts.hold = false;
+		}
+
 		if (opts.delay_ms)
 			sleep_ms(opts.delay_ms);
 
@@ -364,7 +381,7 @@ static void parse_opts(int argc, char **argv)
 {
 	int c;
 
-	while ((c = getopt(argc, argv, "chvoObt:q:Q:d:G")) != -1) {
+	while ((c = getopt(argc, argv, "chvoObt:q:Q:d:GH")) != -1) {
 		switch (c) {
 		case 'c':
 			opts.count_packets = true;
@@ -373,6 +390,9 @@ static void parse_opts(int argc, char **argv)
 			help(argv[0]);
 			exit(0);
 			break;
+		case 'H':
+			opts.hold = true;
+			break;
 		case 'q':
 			opts.queue_num = atoi(optarg);
 			if (opts.queue_num > 0xffff)
tools/testing/selftests/net/netfilter/nft_queue.sh +108 -0

120 lines not on this page · raw message

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#2 lore raw
In reply to Antonio Ojea

Hi Pablo,

Gentle ping. v2 addresses Florian's feedback on v1: rebased on nf-next and the nf_queue_nf_hook_drop() call in nft_ct.c converted as well.

I rebased and retested the series today on nf-next main (a5e7d8e446af):
it applies cleanly, builds, and nft_queue.sh passes, including the new test_hook_drop_scope.

Is there a chance to pick it up for nf-next before the 7.4 merge window, or do you prefer that I resend once 7.4-rc1 is out?

Thanks,
Antonio

maintainer
#3 lore raw
Quoted 6 lines · Antonio Ojea

Gentle ping. v2 addresses Florian's feedback on v1: rebased on nf-next and the nf_queue_nf_hook_drop() call in nft_ct.c converted as well.

I rebased and retested the series today on nf-next main (a5e7d8e446af):
it applies cleanly, builds, and nft_queue.sh passes, including the new test_hook_drop_scope.

I have no more feedback, LGTM.

#4 lore raw
In reply to Antonio Ojea

Hi Antonio,

Quoted 11 lines · Antonio Ojea

Hi Pablo,

Gentle ping. v2 addresses Florian's feedback on v1: rebased on nf-next and the nf_queue_nf_hook_drop() call in nft_ct.c converted as well.

I rebased and retested the series today on nf-next main (a5e7d8e446af):
it applies cleanly, builds, and nft_queue.sh passes, including the new test_hook_drop_scope.

Is there a chance to pick it up for nf-next before the 7.4 merge window, or do you prefer that I resend once 7.4-rc1 is out?

This is now in the net-next tree:

https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git/commit/?id=5d1b850ea3f6e6ba2090ebc437e9bbec1d7e5ae4

according to:

https://lore.kernel.org/netfilter-devel/atXgURtsFB4UFXES@chamomile/T/#u

this needs a follow up. Florian, please, confirm this.

Thanks.

maintainer
#5 lore raw
Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines · Antonio Ojea

Is there a chance to pick it up for nf-next before the 7.4 merge window, or do you prefer that I resend once 7.4-rc1 is out?

This is now in the net-next tree:

Quoted 7 lines

https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git/commit/?id=5d1b850ea3f6e6ba2090ebc437e9bbec1d7e5ae4

according to:

https://lore.kernel.org/netfilter-devel/atXgURtsFB4UFXES@chamomile/T/#u

this needs a follow up. Florian, please, confirm this.

Yes, this needs a followup, both br_netfilter and conntrack should flush nfqueued packets explicitly.

Antonio, could you make such a patch? Thanks.

#6 lore raw
Quoted 12 lines · Florian Westphal
Quoted 10 lines · Pablo Neira Ayuso
Quoted 2 lines · Antonio Ojea

Is there a chance to pick it up for nf-next before the 7.4 merge window, or do you prefer that I resend once 7.4-rc1 is out?

This is now in the net-next tree:

https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next.git/commit/?id=5d1b850ea3f6e6ba2090ebc437e9bbec1d7e5ae4

according to:

Quoted 8 lines
Quoted 3 lines

https://lore.kernel.org/netfilter-devel/atXgURtsFB4UFXES@chamomile/T/#u

this needs a follow up. Florian, please, confirm this.

Yes, this needs a followup, both br_netfilter and conntrack should flush nfqueued packets explicitly.

Antonio, could you make such a patch? Thanks.

Acknowledged, I will do it