All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Cc: mingqian591@gmail.com
Subject: [PATCH nf] netfilter: nf_tables: release flowtable after rcu grace period on error
Date: Tue, 17 Mar 2026 18:59:52 +0100	[thread overview]
Message-ID: <20260317175952.26821-1-pablo@netfilter.org> (raw)

Use kfree_rcu() to release flowtable from error path, since a hook that
already refers to this flowtable can be already registered, exposing
this flowtable to packet path and nfnetlink_hook control plane.

Uncovered by KASAN reported as use-after-free from nfnetlink_hook path
when dumping hooks.

The number of flowtable objects in a ruleset are expected to be small,
the increment is memory consumption should be negligible. In older
kernels, users could mistype device names leading to this error path,
I prefer struct rcu_head here instead of explicit synchronize_rcu()
call.

Fixes: 3b49e2e94e6e ("netfilter: nf_tables: add flow table netlink frontend")
Reported-by: Yiming Qian <yimingqian591@gmail.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 include/net/netfilter/nf_tables.h | 2 ++
 net/netfilter/nf_tables_api.c     | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/include/net/netfilter/nf_tables.h b/include/net/netfilter/nf_tables.h
index 3c8a60ec1cc4..ae9905b5ba72 100644
--- a/include/net/netfilter/nf_tables.h
+++ b/include/net/netfilter/nf_tables.h
@@ -1483,6 +1483,7 @@ void nft_unregister_obj(struct nft_object_type *obj_type);
  *	@genmask: generation mask
  *	@use: number of references to this flow table
  * 	@handle: unique object handle
+ *	@rcu_head: deferred release for error path
  *	@hook_list: hook list for hooks per net_device in flowtables
  *	@data: rhashtable and garbage collector
  */
@@ -1495,6 +1496,7 @@ struct nft_flowtable {
 	u32				genmask:2;
 	u32				use;
 	u64				handle;
+	struct rcu_head			rcu_head;
 	/* runtime data below here */
 	struct list_head		hook_list ____cacheline_aligned;
 	struct nf_flowtable		data;
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
index 2f19c155069e..16b80e17247f 100644
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ -9229,7 +9229,7 @@ static int nf_tables_newflowtable(struct sk_buff *skb,
 err2:
 	kfree(flowtable->name);
 err1:
-	kfree(flowtable);
+	kfree_rcu(flowtable, rcu_head);
 flowtable_alloc:
 	nft_use_dec_restore(&table->use);
 
-- 
2.47.3


             reply	other threads:[~2026-03-17 17:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-17 17:59 Pablo Neira Ayuso [this message]
2026-03-17 18:20 ` [PATCH nf] netfilter: nf_tables: release flowtable after rcu grace period on error 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=20260317175952.26821-1-pablo@netfilter.org \
    --to=pablo@netfilter.org \
    --cc=mingqian591@gmail.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.