{"id":2233359,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2233359/?format=json","web_url":"http://patchwork.ozlabs.org/project/netfilter-devel/patch/20260506100728.2664-6-fw@strlen.de/","project":{"id":26,"url":"http://patchwork.ozlabs.org/api/1.1/projects/26/?format=json","name":"Netfilter Development","link_name":"netfilter-devel","list_id":"netfilter-devel.vger.kernel.org","list_email":"netfilter-devel@vger.kernel.org","web_url":null,"scm_url":null,"webscm_url":null},"msgid":"<20260506100728.2664-6-fw@strlen.de>","date":"2026-05-06T10:07:17","name":"[v3,nf,5/8] netfilter: x_tables: add and use xtables_unregister_table_exit","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"d0184db752e6cb9d3e6bf107530b959bbe9174b9","submitter":{"id":1025,"url":"http://patchwork.ozlabs.org/api/1.1/people/1025/?format=json","name":"Florian Westphal","email":"fw@strlen.de"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/netfilter-devel/patch/20260506100728.2664-6-fw@strlen.de/mbox/","series":[{"id":502948,"url":"http://patchwork.ozlabs.org/api/1.1/series/502948/?format=json","web_url":"http://patchwork.ozlabs.org/project/netfilter-devel/list/?series=502948","date":"2026-05-06T10:07:14","name":"netfilter: xtables: fix module load and teardown races","version":3,"mbox":"http://patchwork.ozlabs.org/series/502948/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2233359/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2233359/checks/","tags":{},"headers":{"Return-Path":"\n <netfilter-devel+bounces-12456-incoming=patchwork.ozlabs.org@vger.kernel.org>","X-Original-To":["incoming@patchwork.ozlabs.org","netfilter-devel@vger.kernel.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=2600:3c0a:e001:db::12fc:5321; helo=sea.lore.kernel.org;\n envelope-from=netfilter-devel+bounces-12456-incoming=patchwork.ozlabs.org@vger.kernel.org;\n receiver=patchwork.ozlabs.org)","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=91.216.245.30","smtp.subspace.kernel.org;\n dmarc=none (p=none dis=none) header.from=strlen.de","smtp.subspace.kernel.org;\n spf=pass smtp.mailfrom=Chamillionaire.breakpoint.cc"],"Received":["from sea.lore.kernel.org (sea.lore.kernel.org\n [IPv6:2600:3c0a:e001:db::12fc:5321])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g9WKh3sGHz1yJx\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 06 May 2026 20:08:08 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sea.lore.kernel.org (Postfix) with ESMTP id F0247300A382\n\tfor <incoming@patchwork.ozlabs.org>; Wed,  6 May 2026 10:08:02 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id 108803F9F3E;\n\tWed,  6 May 2026 10:08:02 +0000 (UTC)","from Chamillionaire.breakpoint.cc (Chamillionaire.breakpoint.cc\n [91.216.245.30])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id 1071E19DF6A\n\tfor <netfilter-devel@vger.kernel.org>; Wed,  6 May 2026 10:07:57 +0000 (UTC)","by Chamillionaire.breakpoint.cc (Postfix, from userid 1003)\n\tid BC99260AED; Wed, 06 May 2026 12:07:55 +0200 (CEST)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1778062081; cv=none;\n b=CCXgDr6UUYoPAuvBbKVi1g1Zgko/lWInJlymeQ6TedpgM0M1feS5Zh2jlqT2Cx4PWe6MsfFLPIq5hM1OPRUsW8UGW2NgGqHQRXD+X8GPz/WBWeYHL0ZM/qB6IKt/azYyjltkJo7biEB8FB2pAw+W/l51GqbJ/OMDwSREsNdYYtA=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1778062081; c=relaxed/simple;\n\tbh=4+z1ma87z0PNh3lC0vgWvwyAP1+Poo78zvsGCTkuG7M=;\n\th=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:\n\t MIME-Version;\n b=ZTw76gCER3x3/gvKmoNDWVJbYwY3CmQPtrNL8kVIM42meCkvmpu6vwVnSvVm639gLi9u/GI7mwl1kiAP/KcDo2Dk6LuGa2FD38v55qVmzkZ3ylq1iGRejm4ivNZipqo5eeBa+vwC/diOAdIarGfwGlrJbGUy/q8+jWgcXnx4BSc=","ARC-Authentication-Results":"i=1; smtp.subspace.kernel.org;\n dmarc=none (p=none dis=none) header.from=strlen.de;\n spf=pass smtp.mailfrom=Chamillionaire.breakpoint.cc;\n arc=none smtp.client-ip=91.216.245.30","From":"Florian Westphal <fw@strlen.de>","To":"<netfilter-devel@vger.kernel.org>","Cc":"tristan@talencesecurity.com,\n\tFlorian Westphal <fw@strlen.de>","Subject":"[PATCH v3 nf 5/8] netfilter: x_tables: add and use\n xtables_unregister_table_exit","Date":"Wed,  6 May 2026 12:07:17 +0200","Message-ID":"<20260506100728.2664-6-fw@strlen.de>","X-Mailer":"git-send-email 2.53.0","In-Reply-To":"<20260506100728.2664-1-fw@strlen.de>","References":"<20260506100728.2664-1-fw@strlen.de>","Precedence":"bulk","X-Mailing-List":"netfilter-devel@vger.kernel.org","List-Id":"<netfilter-devel.vger.kernel.org>","List-Subscribe":"<mailto:netfilter-devel+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:netfilter-devel+unsubscribe@vger.kernel.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit"},"content":"Previous change added xtables_unregister_table_pre_exit to detach the\ntable from the packetpath and to unlink it from the active table list.\nIn case of rmmod, userspace that is doing set/getsockopt for this table\nwill not be able to re-instantiate the table:\n 1. The larval table has been removed already\n 2. existing instantiated table is no longer on the xt pernet table list.\n\nThis adds the second stage helper:\n\nunlink the table from the dying list, free the hook ops (if any) and do\nthe audit notification.  It replaces xt_unregister_table().\n\nFixes: fdacd57c79b7 (\"netfilter: x_tables: never register tables by default\")\nReported-by: Tristan Madani <tristan@talencesecurity.com>\nReviewed-by: Tristan Madani <tristan@talencesecurity.com>\nCloses: https://lore.kernel.org/netfilter-devel/20260429175613.1459342-1-tristmd@gmail.com/\nSigned-off-by: Florian Westphal <fw@strlen.de>\n---\n v3: no changes.\n include/linux/netfilter/x_tables.h |  2 +-\n net/ipv4/netfilter/arp_tables.c    |  9 ++--\n net/ipv4/netfilter/ip_tables.c     |  9 ++--\n net/ipv4/netfilter/iptable_nat.c   |  5 +-\n net/ipv6/netfilter/ip6_tables.c    |  9 ++--\n net/ipv6/netfilter/ip6table_nat.c  |  5 +-\n net/netfilter/x_tables.c           | 81 +++++++++++++++++++++++-------\n 7 files changed, 83 insertions(+), 37 deletions(-)","diff":"diff --git a/include/linux/netfilter/x_tables.h b/include/linux/netfilter/x_tables.h\nindex 74486714ae20..5a1c5c336fa4 100644\n--- a/include/linux/netfilter/x_tables.h\n+++ b/include/linux/netfilter/x_tables.h\n@@ -308,8 +308,8 @@ struct xt_table *xt_register_table(struct net *net,\n \t\t\t\t   const struct nf_hook_ops *template_ops,\n \t\t\t\t   struct xt_table_info *bootstrap,\n \t\t\t\t   struct xt_table_info *newinfo);\n-void *xt_unregister_table(struct xt_table *table);\n void xt_unregister_table_pre_exit(struct net *net, u8 af, const char *name);\n+struct xt_table *xt_unregister_table_exit(struct net *net, u8 af, const char *name);\n \n struct xt_table_info *xt_replace_table(struct xt_table *table,\n \t\t\t\t       unsigned int num_counters,\ndiff --git a/net/ipv4/netfilter/arp_tables.c b/net/ipv4/netfilter/arp_tables.c\nindex bd348b7bad2c..ad2259678c78 100644\n--- a/net/ipv4/netfilter/arp_tables.c\n+++ b/net/ipv4/netfilter/arp_tables.c\n@@ -1501,13 +1501,11 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len\n \n static void __arpt_unregister_table(struct net *net, struct xt_table *table)\n {\n-\tstruct xt_table_info *private;\n-\tvoid *loc_cpu_entry;\n+\tstruct xt_table_info *private = table->private;\n \tstruct module *table_owner = table->me;\n+\tvoid *loc_cpu_entry;\n \tstruct arpt_entry *iter;\n \n-\tprivate = xt_unregister_table(table);\n-\n \t/* Decrease module usage counts and free resources */\n \tloc_cpu_entry = private->entries;\n \txt_entry_foreach(iter, loc_cpu_entry, private->size)\n@@ -1515,6 +1513,7 @@ static void __arpt_unregister_table(struct net *net, struct xt_table *table)\n \tif (private->number > private->initial_entries)\n \t\tmodule_put(table_owner);\n \txt_free_table_info(private);\n+\tkfree(table);\n }\n \n int arpt_register_table(struct net *net,\n@@ -1556,7 +1555,7 @@ int arpt_register_table(struct net *net,\n \n void arpt_unregister_table(struct net *net, const char *name)\n {\n-\tstruct xt_table *table = xt_find_table(net, NFPROTO_ARP, name);\n+\tstruct xt_table *table = xt_unregister_table_exit(net, NFPROTO_ARP, name);\n \n \tif (table)\n \t\t__arpt_unregister_table(net, table);\ndiff --git a/net/ipv4/netfilter/ip_tables.c b/net/ipv4/netfilter/ip_tables.c\nindex 864489928fb5..5cbdb0815857 100644\n--- a/net/ipv4/netfilter/ip_tables.c\n+++ b/net/ipv4/netfilter/ip_tables.c\n@@ -1704,12 +1704,10 @@ do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)\n \n static void __ipt_unregister_table(struct net *net, struct xt_table *table)\n {\n-\tstruct xt_table_info *private;\n-\tvoid *loc_cpu_entry;\n+\tstruct xt_table_info *private = table->private;\n \tstruct module *table_owner = table->me;\n \tstruct ipt_entry *iter;\n-\n-\tprivate = xt_unregister_table(table);\n+\tvoid *loc_cpu_entry;\n \n \t/* Decrease module usage counts and free resources */\n \tloc_cpu_entry = private->entries;\n@@ -1718,6 +1716,7 @@ static void __ipt_unregister_table(struct net *net, struct xt_table *table)\n \tif (private->number > private->initial_entries)\n \t\tmodule_put(table_owner);\n \txt_free_table_info(private);\n+\tkfree(table);\n }\n \n int ipt_register_table(struct net *net, const struct xt_table *table,\n@@ -1758,7 +1757,7 @@ int ipt_register_table(struct net *net, const struct xt_table *table,\n \n void ipt_unregister_table_exit(struct net *net, const char *name)\n {\n-\tstruct xt_table *table = xt_find_table(net, NFPROTO_IPV4, name);\n+\tstruct xt_table *table = xt_unregister_table_exit(net, NFPROTO_IPV4, name);\n \n \tif (table)\n \t\t__ipt_unregister_table(net, table);\ndiff --git a/net/ipv4/netfilter/iptable_nat.c b/net/ipv4/netfilter/iptable_nat.c\nindex 8fc4912e790d..a0df72554025 100644\n--- a/net/ipv4/netfilter/iptable_nat.c\n+++ b/net/ipv4/netfilter/iptable_nat.c\n@@ -119,8 +119,11 @@ static int iptable_nat_table_init(struct net *net)\n \t}\n \n \tret = ipt_nat_register_lookups(net);\n-\tif (ret < 0)\n+\tif (ret < 0) {\n+\t\txt_unregister_table_pre_exit(net, NFPROTO_IPV4, \"nat\");\n+\t\tsynchronize_rcu();\n \t\tipt_unregister_table_exit(net, \"nat\");\n+\t}\n \n \tkfree(repl);\n \treturn ret;\ndiff --git a/net/ipv6/netfilter/ip6_tables.c b/net/ipv6/netfilter/ip6_tables.c\nindex edf50bc7787e..9d9c3763f2f5 100644\n--- a/net/ipv6/netfilter/ip6_tables.c\n+++ b/net/ipv6/netfilter/ip6_tables.c\n@@ -1713,12 +1713,10 @@ do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)\n \n static void __ip6t_unregister_table(struct net *net, struct xt_table *table)\n {\n-\tstruct xt_table_info *private;\n-\tvoid *loc_cpu_entry;\n+\tstruct xt_table_info *private = table->private;\n \tstruct module *table_owner = table->me;\n \tstruct ip6t_entry *iter;\n-\n-\tprivate = xt_unregister_table(table);\n+\tvoid *loc_cpu_entry;\n \n \t/* Decrease module usage counts and free resources */\n \tloc_cpu_entry = private->entries;\n@@ -1727,6 +1725,7 @@ static void __ip6t_unregister_table(struct net *net, struct xt_table *table)\n \tif (private->number > private->initial_entries)\n \t\tmodule_put(table_owner);\n \txt_free_table_info(private);\n+\tkfree(table);\n }\n \n int ip6t_register_table(struct net *net, const struct xt_table *table,\n@@ -1767,7 +1766,7 @@ int ip6t_register_table(struct net *net, const struct xt_table *table,\n \n void ip6t_unregister_table_exit(struct net *net, const char *name)\n {\n-\tstruct xt_table *table = xt_find_table(net, NFPROTO_IPV6, name);\n+\tstruct xt_table *table = xt_unregister_table_exit(net, NFPROTO_IPV6, name);\n \n \tif (table)\n \t\t__ip6t_unregister_table(net, table);\ndiff --git a/net/ipv6/netfilter/ip6table_nat.c b/net/ipv6/netfilter/ip6table_nat.c\nindex bb8aa3fc42b4..c2394e2c94b5 100644\n--- a/net/ipv6/netfilter/ip6table_nat.c\n+++ b/net/ipv6/netfilter/ip6table_nat.c\n@@ -121,8 +121,11 @@ static int ip6table_nat_table_init(struct net *net)\n \t}\n \n \tret = ip6t_nat_register_lookups(net);\n-\tif (ret < 0)\n+\tif (ret < 0) {\n+\t\txt_unregister_table_pre_exit(net, NFPROTO_IPV6, \"nat\");\n+\t\tsynchronize_rcu();\n \t\tip6t_unregister_table_exit(net, \"nat\");\n+\t}\n \n \tkfree(repl);\n \treturn ret;\ndiff --git a/net/netfilter/x_tables.c b/net/netfilter/x_tables.c\nindex 9c1e896c7b03..4e6708c23922 100644\n--- a/net/netfilter/x_tables.c\n+++ b/net/netfilter/x_tables.c\n@@ -55,6 +55,9 @@ static struct list_head xt_templates[NFPROTO_NUMPROTO];\n \n struct xt_pernet {\n \tstruct list_head tables[NFPROTO_NUMPROTO];\n+\n+\t/* stash area used during netns exit */\n+\tstruct list_head dead_tables[NFPROTO_NUMPROTO];\n };\n \n struct compat_delta {\n@@ -1634,23 +1637,6 @@ struct xt_table *xt_register_table(struct net *net,\n }\n EXPORT_SYMBOL_GPL(xt_register_table);\n \n-void *xt_unregister_table(struct xt_table *table)\n-{\n-\tstruct xt_table_info *private;\n-\n-\tmutex_lock(&xt[table->af].mutex);\n-\tprivate = table->private;\n-\tlist_del(&table->list);\n-\tmutex_unlock(&xt[table->af].mutex);\n-\taudit_log_nfcfg(table->name, table->af, private->number,\n-\t\t\tAUDIT_XT_OP_UNREGISTER, GFP_KERNEL);\n-\tkfree(table->ops);\n-\tkfree(table);\n-\n-\treturn private;\n-}\n-EXPORT_SYMBOL_GPL(xt_unregister_table);\n-\n /**\n  * xt_unregister_table_pre_exit - pre-shutdown unregister of a table\n  * @net: network namespace\n@@ -1660,6 +1646,14 @@ EXPORT_SYMBOL_GPL(xt_unregister_table);\n  * Unregisters the specified netfilter table from the given network namespace\n  * and also unregisters the hooks from netfilter core: no new packets will be\n  * processed.\n+ *\n+ * This must be called prior to xt_unregister_table_exit() from the pernet\n+ * .pre_exit callback.  After this call, the table is no longer visible to\n+ * the get/setsockopt path.  In case of rmmod, module exit path must have\n+ * called xt_unregister_template() prior to unregistering pernet ops to\n+ * prevent re-instantiation of the table.\n+ *\n+ * See also: xt_unregister_table_exit()\n  */\n void xt_unregister_table_pre_exit(struct net *net, u8 af, const char *name)\n {\n@@ -1669,6 +1663,7 @@ void xt_unregister_table_pre_exit(struct net *net, u8 af, const char *name)\n \tmutex_lock(&xt[af].mutex);\n \tlist_for_each_entry(t, &xt_net->tables[af], list) {\n \t\tif (strcmp(t->name, name) == 0) {\n+\t\t\tlist_move(&t->list, &xt_net->dead_tables[af]);\n \t\t\tmutex_unlock(&xt[af].mutex);\n \n \t\t\tif (t->ops) /* nat table registers with nat core, t->ops is NULL. */\n@@ -1679,6 +1674,50 @@ void xt_unregister_table_pre_exit(struct net *net, u8 af, const char *name)\n \tmutex_unlock(&xt[af].mutex);\n }\n EXPORT_SYMBOL(xt_unregister_table_pre_exit);\n+\n+/**\n+ * xt_unregister_table_exit - remove a table during namespace teardown\n+ * @net: the network namespace from which to unregister the table\n+ * @af: address family (e.g., NFPROTO_IPV4, NFPROTO_IPV6)\n+ * @name: name of the table to unregister\n+ *\n+ * Completes the unregister process for a table. This must be called from\n+ * the pernet ops .exit callback. This is the second stage after\n+ * xt_unregister_table_pre_exit().\n+ *\n+ * pair with xt_unregister_table_pre_exit() during namespace shutdown.\n+ *\n+ * Return: the unregistered table or NULL if the table was never\n+ *         instantiated. The caller needs to kfree() the table after it\n+ *         has removed the family specific matches/targets.\n+ */\n+struct xt_table *xt_unregister_table_exit(struct net *net, u8 af, const char *name)\n+{\n+\tstruct xt_pernet *xt_net = net_generic(net, xt_pernet_id);\n+\tstruct xt_table *table;\n+\n+\tmutex_lock(&xt[af].mutex);\n+\tlist_for_each_entry(table, &xt_net->dead_tables[af], list) {\n+\t\tstruct nf_hook_ops *ops = NULL;\n+\n+\t\tif (strcmp(table->name, name) != 0)\n+\t\t\tcontinue;\n+\n+\t\tlist_del(&table->list);\n+\n+\t\taudit_log_nfcfg(table->name, table->af, table->private->number,\n+\t\t\t\tAUDIT_XT_OP_UNREGISTER, GFP_KERNEL);\n+\t\tswap(table->ops, ops);\n+\t\tmutex_unlock(&xt[af].mutex);\n+\n+\t\tkfree(ops);\n+\t\treturn table;\n+\t}\n+\tmutex_unlock(&xt[af].mutex);\n+\n+\treturn NULL;\n+}\n+EXPORT_SYMBOL_GPL(xt_unregister_table_exit);\n #endif\n \n #ifdef CONFIG_PROC_FS\n@@ -2125,8 +2164,10 @@ static int __net_init xt_net_init(struct net *net)\n \tstruct xt_pernet *xt_net = net_generic(net, xt_pernet_id);\n \tint i;\n \n-\tfor (i = 0; i < NFPROTO_NUMPROTO; i++)\n+\tfor (i = 0; i < NFPROTO_NUMPROTO; i++) {\n \t\tINIT_LIST_HEAD(&xt_net->tables[i]);\n+\t\tINIT_LIST_HEAD(&xt_net->dead_tables[i]);\n+\t}\n \treturn 0;\n }\n \n@@ -2135,8 +2176,10 @@ static void __net_exit xt_net_exit(struct net *net)\n \tstruct xt_pernet *xt_net = net_generic(net, xt_pernet_id);\n \tint i;\n \n-\tfor (i = 0; i < NFPROTO_NUMPROTO; i++)\n+\tfor (i = 0; i < NFPROTO_NUMPROTO; i++) {\n \t\tWARN_ON_ONCE(!list_empty(&xt_net->tables[i]));\n+\t\tWARN_ON_ONCE(!list_empty(&xt_net->dead_tables[i]));\n+\t}\n }\n \n static struct pernet_operations xt_net_ops = {\n","prefixes":["v3","nf","5/8"]}