Linux Netfilter development
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Subject: [PATCH nf 2/2] netfilter: flowtable: bump ct refcount before teardown
Date: Thu, 27 Aug 2026 01:32:15 +0200	[thread overview]
Message-ID: <20260826233215.279277-2-pablo@netfilter.org> (raw)
In-Reply-To: <20260826233215.279277-1-pablo@netfilter.org>

The existing ct state and timeout fix up after setting the teardown bit
is unsafe. The flowtable GC owns the flow entry once the teardown bit is
set on. Therefore, it might release the flow entry and drop the
reference on the ct while the ct fix up is being performed from either
packet path or netdevice notifier.

Bump the ct refcount, in combination with the teardown bit, as a
synchronization point, if refcount is zero, then this flow is being
teardown. Furthermore, if the teardown bit has been already set, then
this CPU lost race to tear down this flow.

Fixes: d2d31ea8cd80 ("netfilter: conntrack: fix erronous removal of offload bit")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
v1: new in this series.

 net/netfilter/nf_flow_table_core.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/net/netfilter/nf_flow_table_core.c b/net/netfilter/nf_flow_table_core.c
index 625caaa8addf..1a9fd5442b0c 100644
--- a/net/netfilter/nf_flow_table_core.c
+++ b/net/netfilter/nf_flow_table_core.c
@@ -392,9 +392,15 @@ static void flow_offload_del(struct nf_flowtable *flow_table,
 
 void flow_offload_teardown(struct flow_offload *flow)
 {
-	clear_bit(IPS_OFFLOAD_BIT, &flow->ct->status);
-	if (!test_and_set_bit(NF_FLOW_TEARDOWN, &flow->flags))
+	if (unlikely(!refcount_inc_not_zero(&flow->ct->ct_general.use)))
+		return;
+
+	if (!test_and_set_bit(NF_FLOW_TEARDOWN, &flow->flags)) {
 		flow_offload_fixup_ct(flow);
+		smp_mb__before_atomic();
+		clear_bit(IPS_OFFLOAD_BIT, &flow->ct->status);
+	}
+	nf_ct_put(flow->ct);
 }
 EXPORT_SYMBOL_GPL(flow_offload_teardown);
 
-- 
2.47.3


      reply	other threads:[~2026-08-26 23:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-26 23:32 [PATCH nf,v2 1/2] netfilter: flowtable: defer ct stats sync via worker Pablo Neira Ayuso
2026-08-26 23:32 ` Pablo Neira Ayuso [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260826233215.279277-2-pablo@netfilter.org \
    --to=pablo@netfilter.org \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox