Netdev List
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: davem@davemloft.net, netdev@vger.kernel.org, kuba@kernel.org,
	pabeni@redhat.com, edumazet@kernel.org, horms@kernel.org,
	fw@strlen.de, ja@ssi.bg
Subject: [PATCH net-next 01/15] netfilter: conntrack: prevent nf_conntrack_buckets race condition
Date: Mon, 23 Nov 2026 14:57:12 +0100	[thread overview]
Message-ID: <20261123135726.3940688-2-pablo@netfilter.org> (raw)
In-Reply-To: <20261123135726.3940688-1-pablo@netfilter.org>

From: Fernando Fernandez Mancera <fmancera@suse.de>

There is a race condition in the nf_conntrack_buckets sysctl handler.
The implementation uses a single global variable as a temporary buffer
to parse user input. Because sysctl handlers are not locked, a
concurrent read or write can overwrite this shared variable after a
writer has parsed its input but before the actual resize occurs.

While this sysctl is only writable from the initial network namespace,
the race can trigger if a monitoring daemon reads the sysctl while an
administrator or orchestrator is writing to it.

Fix this by removing the unnecessary global state by using a local-stack
table instead and delegating the locking/serialization to
nf_conntrack_hash_resize() function.

Fixes: 3183ab8997a4 ("netfilter: conntrack: allow increasing bucket size via sysctl too")
Reported-by: VEGA <vega@nebusec.ai>
Signed-off-by: Fernando Fernandez Mancera <fmancera@suse.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 net/netfilter/nf_conntrack_standalone.c | 18 +++++-------------
 1 file changed, 5 insertions(+), 13 deletions(-)

diff --git a/net/netfilter/nf_conntrack_standalone.c b/net/netfilter/nf_conntrack_standalone.c
index d88a10230862..9775efe4e2c9 100644
--- a/net/netfilter/nf_conntrack_standalone.c
+++ b/net/netfilter/nf_conntrack_standalone.c
@@ -533,27 +533,22 @@ EXPORT_SYMBOL_GPL(nf_conntrack_count);
 /* Sysctl support */
 
 #ifdef CONFIG_SYSCTL
-/* size the user *wants to set */
-static unsigned int nf_conntrack_htable_size_user __read_mostly;
-
 static int
 nf_conntrack_hash_sysctl(const struct ctl_table *table, int write,
 			 void *buffer, size_t *lenp, loff_t *ppos)
 {
+	unsigned int htable_size = READ_ONCE(nf_conntrack_htable_size);
+	struct ctl_table tmp = *table;
 	int ret;
 
-	/* module_param hashsize could have changed value */
-	nf_conntrack_htable_size_user = nf_conntrack_htable_size;
+	tmp.data = &htable_size;
 
-	ret = proc_dointvec(table, write, buffer, lenp, ppos);
+	ret = proc_dointvec(&tmp, write, buffer, lenp, ppos);
 	if (ret < 0 || !write)
 		return ret;
 
 	/* update ret, we might not be able to satisfy request */
-	ret = nf_conntrack_hash_resize(nf_conntrack_htable_size_user);
-
-	/* update it to the actual value used by conntrack */
-	nf_conntrack_htable_size_user = nf_conntrack_htable_size;
+	ret = nf_conntrack_hash_resize(htable_size);
 	return ret;
 }
 
@@ -657,7 +652,6 @@ static const struct ctl_table nf_ct_sysctl_table[] = {
 	},
 	[NF_SYSCTL_CT_BUCKETS] = {
 		.procname       = "nf_conntrack_buckets",
-		.data           = &nf_conntrack_htable_size_user,
 		.maxlen         = sizeof(unsigned int),
 		.mode           = 0644,
 		.proc_handler   = nf_conntrack_hash_sysctl,
@@ -1153,8 +1147,6 @@ static int __init nf_conntrack_standalone_init(void)
 		ret = -ENOMEM;
 		goto out_sysctl;
 	}
-
-	nf_conntrack_htable_size_user = nf_conntrack_htable_size;
 #endif
 
 	nf_conntrack_init_end();
-- 
2.47.3


  reply	other threads:[~2026-11-23 13:57 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 13:57 [PATCH net-next,v2 00/15] Netfilter updates for net-next Pablo Neira Ayuso
2026-11-23 13:57 ` Pablo Neira Ayuso [this message]
2026-11-23 13:57 ` [PATCH net-next 02/15] netfilter: conntrack: validate skb->_nfct and packet headers Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 03/15] netfilter: conntrack: refactor helper call logic in nf_confirm() Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 04/15] netfilter: conntrack: verify L4 protocol before calling helper Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 05/15] netfilter: conntrack: replace open-coded helper invocation Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 06/15] netfilter: conntrack: harden helper invocation with tuple revalidation Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 07/15] netfilter: nft_ct: validate timeout object protocol Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 08/15] netfilter: nft_synproxy: only handle pure SYN and ACK packets Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 09/15] netfilter: nft_ct: validate hook and priority for ct zone set Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 10/15] rhashtable: Add rhashtable_flush_and_free helper Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 11/15] netfilter: ipset: add rhashtable boilerplate stubs Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 12/15] netfilter: ipset: replace internal hash table with rhashtable Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 13/15] netfilter: ipset: also report mem size for cidr storage to userspace Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 14/15] netfilter: ipset: re-add IP_SET_HASH_WITH_MULTI support Pablo Neira Ayuso
2026-11-23 13:57 ` [PATCH net-next 15/15] netfilter: ipset: re-add forceadd support Pablo Neira Ayuso
  -- strict thread matches above, loose matches on Subject: below --
2026-11-23 13:54 [PATCH net-next 00/15] Netfilter updates for net-next Pablo Neira Ayuso
2026-11-23 13:54 ` [PATCH net-next 01/15] netfilter: conntrack: prevent nf_conntrack_buckets race condition Pablo Neira Ayuso

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=20261123135726.3940688-2-pablo@netfilter.org \
    --to=pablo@netfilter.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=horms@kernel.org \
    --cc=ja@ssi.bg \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /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