{"id":2232017,"url":"http://patchwork.ozlabs.org/api/1.2/patches/2232017/?format=json","web_url":"http://patchwork.ozlabs.org/project/netfilter-devel/patch/20260502075639.7440-5-fw@strlen.de/","project":{"id":26,"url":"http://patchwork.ozlabs.org/api/1.2/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,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20260502075639.7440-5-fw@strlen.de>","list_archive_url":null,"date":"2026-05-02T07:56:38","name":"[nf,4/5] netfilter: x_tables: add and use xtables_unregister_table_exit","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"443d6d894683c8ec36c25ee7cf7f4bb309bc51e6","submitter":{"id":1025,"url":"http://patchwork.ozlabs.org/api/1.2/people/1025/?format=json","name":"Florian Westphal","email":"fw@strlen.de"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/netfilter-devel/patch/20260502075639.7440-5-fw@strlen.de/mbox/","series":[{"id":502515,"url":"http://patchwork.ozlabs.org/api/1.2/series/502515/?format=json","web_url":"http://patchwork.ozlabs.org/project/netfilter-devel/list/?series=502515","date":"2026-05-02T07:56:34","name":"netfilter: xtables: fix module unload and teardown races","version":1,"mbox":"http://patchwork.ozlabs.org/series/502515/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2232017/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2232017/checks/","tags":{},"related":[],"headers":{"Return-Path":"\n <netfilter-devel+bounces-12393-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=172.234.253.10; helo=sea.lore.kernel.org;\n envelope-from=netfilter-devel+bounces-12393-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 [172.234.253.10])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g70cT6vqQz1yJ0\n\tfor <incoming@patchwork.ozlabs.org>; Sat, 02 May 2026 17:57:13 +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 D8495302172E\n\tfor <incoming@patchwork.ozlabs.org>; Sat,  2 May 2026 07:57:06 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id 522603019A4;\n\tSat,  2 May 2026 07:57:06 +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 87F7A2E889C\n\tfor <netfilter-devel@vger.kernel.org>; Sat,  2 May 2026 07:57:04 +0000 (UTC)","by Chamillionaire.breakpoint.cc (Postfix, from userid 1003)\n\tid 18636605BD; Sat, 02 May 2026 09:57:03 +0200 (CEST)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1777708626; cv=none;\n b=EnLA5okTl8NxsNzGYvxYncVp3koAgzSMfV0Rgfmes08UMDenjBSr63QYu7XVQ3eHXSS1axjCo9viK1i1LUF0NDSMcFGJcL+gx0uibt64p8AzqrMAmCKqvng5dKQStpdyHQudGbsx/2Q1uy0ak8mBZtn9ZQWKGHe4B9bpYoGHQDI=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1777708626; c=relaxed/simple;\n\tbh=b1vak7SD4dDjAbNs0AWLp/JSDJFFq8NRvvTeNswPBX4=;\n\th=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:\n\t MIME-Version;\n b=N4xwW71FPkMHpllKa86Od8yRR4LCmlqauERmwIUpaaa9coXVRiD5h/LKfGIrBdKgStbRDP7p7/OV8a6oP6tCbmmejEx9gDaOw8VFuze5mQbDRoxmtkeTcgXTYMLKS12C346pXqkgKQXsK6M6uNHL0ZaWoylLSOBLHk3aqMf8qxQ=","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>,\n\tTristan Madani <tristmd@gmail.com>","Subject":"[PATCH nf 4/5] netfilter: x_tables: add and use\n xtables_unregister_table_exit","Date":"Sat,  2 May 2026 09:56:38 +0200","Message-ID":"<20260502075639.7440-5-fw@strlen.de>","X-Mailer":"git-send-email 2.53.0","In-Reply-To":"<20260502075639.7440-1-fw@strlen.de>","References":"<20260502075639.7440-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 <tristmd@gmail.com>\nSigned-off-by: Florian Westphal <fw@strlen.de>\n---\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/ipv6/netfilter/ip6_tables.c    |  9 ++--\n net/netfilter/x_tables.c           | 81 +++++++++++++++++++++++-------\n 5 files changed, 75 insertions(+), 35 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/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/netfilter/x_tables.c b/net/netfilter/x_tables.c\nindex ec72ba0da983..47dc1dff2996 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@@ -1621,23 +1624,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@@ -1647,6 +1633,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@@ -1656,6 +1650,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@@ -1666,6 +1661,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@@ -2112,8 +2151,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@@ -2122,8 +2163,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":["nf","4/5"]}