netfilter: xtables: fix module load and teardown races
Subsystems (3)
Florian Westphal
maintainerv2 cover netfilter: xtables: fix module load and teardown races
v2:
sashiko hints that load part is also broken. Templates must be removed first (exit) and added last (init). Patch 8 (new in this version) does this. Patch 7 has been amended to fix ebtables init and exit handlers.
1) Allow initial x_tables table replacement without emitting an audit log message. Delay the register message until after hooks are wired up to avoid unnecessary unregister logs during error unwinding.
2) Fix a NULL dereference by allocating hook ops before adding the table to the per-netns list. Use synchronize_rcu() during error unwinding to ensure the table stops processing packets before teardown. Defer audit log register message until all operations succeed.
3) Refactor xtables to use a single xt_unregister_table_pre_exit function. Eliminate code duplication by centralizing table unregistration logic within the xtables core. ebtables cannot be changed due to incompatibility.
4) Unregister xtables templates before module removal. This prevents a race condition where userspace instantiates a new table after the pernet unreg removed the current table.
5) Add xtables_unregister_table_exit to fully unregister netfilter tables during module removal. Unlink the table from dying lists, then free hook operations.
6) Implement a two-stage removal scheme for ebtables following the x_tables pattern. Assign table->ops while holding the ebt mutex to prevent exposing partially-filled structures.
7) Fix ebtables module initialization race. Register the template last in table initialization functions. Prevent table instantiation before pernet operations are available.
8) Fix a race condition in x_tables module initialization. Ensure pernet ops are fully set up before exposing the table to userspace.
Florian Westphal (8):
netfilter: x_tables: allow initial table replace without emitting
audit log message
netfilter: xtables: allocate hook ops while under mutex
netfilter: x_tables: add and use xt_unregister_table_pre_exit
netfilter: x_tables: unregister the templates first
netfilter: x_tables: add and use xtables_unregister_table_exit
netfilter: ebtables: move to two-stage removal scheme
netfilter: ebtables: close dangling table module init race
netfilter: x_tables: close dangling table module init race
include/linux/netfilter/x_tables.h | 4 +- include/linux/netfilter_arp/arp_tables.h | 1 - include/linux/netfilter_ipv4/ip_tables.h | 1 - include/linux/netfilter_ipv6/ip6_tables.h | 1 - net/bridge/netfilter/ebtable_broute.c | 14 +- net/bridge/netfilter/ebtable_filter.c | 12 +- net/bridge/netfilter/ebtable_nat.c | 12 +- net/bridge/netfilter/ebtables.c | 60 +++++--- net/ipv4/netfilter/arp_tables.c | 53 +------ net/ipv4/netfilter/arptable_filter.c | 27 ++-- net/ipv4/netfilter/ip_tables.c | 59 +------- net/ipv4/netfilter/iptable_filter.c | 27 ++-- net/ipv4/netfilter/iptable_mangle.c | 29 ++-- net/ipv4/netfilter/iptable_nat.c | 6 +- net/ipv4/netfilter/iptable_raw.c | 26 ++-- net/ipv4/netfilter/iptable_security.c | 23 +-- net/ipv6/netfilter/ip6_tables.c | 56 +------ net/ipv6/netfilter/ip6table_filter.c | 26 ++-- net/ipv6/netfilter/ip6table_mangle.c | 27 ++-- net/ipv6/netfilter/ip6table_nat.c | 6 +- net/ipv6/netfilter/ip6table_raw.c | 24 +-- net/ipv6/netfilter/ip6table_security.c | 27 ++-- net/netfilter/x_tables.c | 177 ++++++++++++++++++---- 23 files changed, 359 insertions(+), 339 deletions(-)
Signature · 1 line
2.53.0
Florian Westphal
maintainerPATCH v2 1/8 netfilter: x_tables: allow initial table replace without emitting audit log message
At the moment we emit the audit log a bit too early, which makes it necessary to also emit an unregister log in case we have to unwind errors after possible hook register failure.
Followup patch will be slightly simpler if we can delay the register message until after the hooks have been wired up.
---
v2: new in this series. As-is, we emit both a register and unregister notification in case something goes wrong at table init time. Instead this minor refactor will permit next patch to only emit the success notification.
net/netfilter/x_tables.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-)
net/netfilter/x_tables.c +20 -9
@@ -1472,11 +1472,9 @@ struct xt_counters *xt_counters_alloc(unsigned int counters) }EXPORT_SYMBOL (xt_counters_alloc); -struct xt_table_info * -xt_replace_table (struct xt_table *table, -unsigned int num_counters, -struct xt_table_info *newinfo, -int *error) +static struct xt_table_info * +do_replace_table (struct xt_table *table,unsigned int num_counters, +struct xt_table_info *newinfo,int *error) {struct xt_table_info *private;unsigned int cpu;
@@ -1531,10 +1529,23 @@ xt_replace_table(struct xt_table *table, } } -audit_log_nfcfg (table->name, table->af, private->number, - !private->number ?AUDIT_XT_OP_REGISTER : -AUDIT_XT_OP_REPLACE , -GFP_KERNEL ); +return private; +} + +struct xt_table_info * +xt_replace_table (struct xt_table *table,unsigned int num_counters, +struct xt_table_info *newinfo, +int *error) +{ +struct xt_table_info *private; + + private =do_replace_table (table, num_counters, newinfo, error); +if (private) +audit_log_nfcfg (table->name, table->af, private->number, + !private->number ?AUDIT_XT_OP_REGISTER : +AUDIT_XT_OP_REPLACE , +GFP_KERNEL ); +return private; }EXPORT_SYMBOL_GPL (xt_replace_table);
Signature · 1 line
2.53.0
Florian Westphal
maintainerPATCH v2 2/8 netfilter: xtables: allocate hook ops while under mutex
In reply to Florian Westphalarp/ip(6)t_register_table() add the table to the per-netns list via xt_register_table() before allocating the per-netns hook ops copy via kmemdup_array(). This leaves a window where the table is visible in the list with ops=NULL.
If the pernet exit happens runs concurrently the pre_exit callback finds the table via xt_find_table() and passes the NULL ops pointer to nf_unregister_net_hooks(), causing a NULL dereference:
general protection fault in nf_unregister_net_hooks+0xbc/0x150
RIP: nf_unregister_net_hooks (net/netfilter/core.c:613)
Call Trace:
ipt_unregister_table_pre_exit
iptable_mangle_net_pre_exit
ops_pre_exit_list
cleanup_netFix by moving the ops allocation into the xtables core so the table is never in the list without valid ops. Also ensure the table is no longer processing packets before its torn down on error unwind. nf_register_net_hooks might have published at least one hook; call synchronize_rcu() if there was an error.
audit log register message gets deferred until all operations have passed, this avoids need to emit another ureg message in case of error unwinding.
Based on earlier patch by Tristan Madani.
---
v2: Defer the 'table load' audit message until after we're done. v1 had an issue where we'd fail to emit the delete message in case of error after table registration.
include/linux/netfilter/x_tables.h | 1 + net/ipv4/netfilter/arp_tables.c | 35 +++------------------ net/ipv4/netfilter/ip_tables.c | 41 +++--------------------- net/ipv6/netfilter/ip6_tables.c | 38 +++-------------------- net/netfilter/x_tables.c | 50 +++++++++++++++++++++++++----- 5 files changed, 55 insertions(+), 110 deletions(-)
include/linux/netfilter/x_tables.h +1 -0
@@ -305,6 +305,7 @@ struct xt_counters *xt_counters_alloc(unsigned int counters);struct xt_table *xt_register_table (struct net *net,const struct xt_table *table, +const struct nf_hook_ops *template_ops,struct xt_table_info *bootstrap,struct xt_table_info *newinfo);void *xt_unregister_table (struct xt_table *table);
net/ipv4/netfilter/arp_tables.c +4 -31
@@ -1522,13 +1522,11 @@ int arpt_register_table(struct net *net,const struct arpt_replace *repl, const struct nf_hook_ops *template_ops) { - struct nf_hook_ops *ops; -unsigned int num_ops; -int ret, i; -struct xt_table_info *newinfo; struct xt_table_info bootstrap = {0}; -void *loc_cpu_entry; +struct xt_table_info *newinfo;struct xt_table *new_table; +void *loc_cpu_entry; +int ret; newinfo =xt_alloc_table_info (repl->size);if (!newinfo)
@@ -1543,7 +1541,7 @@ int arpt_register_table(struct net *net,return ret; } - new_table =xt_register_table (net, table, &bootstrap, newinfo); + new_table =xt_register_table (net, table, template_ops, &bootstrap, newinfo);if (IS_ERR (new_table)) {struct arpt_entry *iter;
@@ -1553,31 +1551,6 @@ int arpt_register_table(struct net *net,return PTR_ERR (new_table); } - num_ops =hweight32 (table->valid_hooks); -if (num_ops ==0 ) { - ret = -EINVAL ; - goto out_free; - } - - ops =kmemdup_array (template_ops, num_ops,sizeof (*ops),GFP_KERNEL ); -if (!ops) { - ret = -ENOMEM ; - goto out_free; - } - -for (i =0 ; i < num_ops; i++) - ops[i].priv = new_table; - - new_table->ops = ops; - - ret =nf_register_net_hooks (net, ops, num_ops); -if (ret !=0 ) - goto out_free; - -return ret; - -out_free: -__arpt_unregister_table (net, new_table);return ret; }
net/ipv4/netfilter/ip_tables.c +4 -37
61 lines not on this page · raw message
net/ipv6/netfilter/ip6_tables.c +4 -34
@@ -1733,13 +1733,11 @@ int ip6t_register_table(struct net *net, const struct xt_table *table,const struct ip6t_replace *repl, const struct nf_hook_ops *template_ops) { - struct nf_hook_ops *ops; -unsigned int num_ops; -int ret, i; -struct xt_table_info *newinfo; struct xt_table_info bootstrap = {0}; -void *loc_cpu_entry; +struct xt_table_info *newinfo;struct xt_table *new_table; +void *loc_cpu_entry; +int ret; newinfo =xt_alloc_table_info (repl->size);if (!newinfo)
@@ -1754,7 +1752,7 @@ int ip6t_register_table(struct net *net, const struct xt_table *table,return ret; } - new_table =xt_register_table (net, table, &bootstrap, newinfo); + new_table =xt_register_table (net, table, template_ops, &bootstrap, newinfo);if (IS_ERR (new_table)) {struct ip6t_entry *iter;
@@ -1764,34 +1762,6 @@ int ip6t_register_table(struct net *net, const struct xt_table *table,return PTR_ERR (new_table); } -if (!template_ops) -return 0 ; - - num_ops =hweight32 (table->valid_hooks); -if (num_ops ==0 ) { - ret = -EINVAL ; - goto out_free; - } - - ops =kmemdup_array (template_ops, num_ops,sizeof (*ops),GFP_KERNEL ); -if (!ops) { - ret = -ENOMEM ; - goto out_free; - } - -for (i =0 ; i < num_ops; i++) - ops[i].priv = new_table; - - new_table->ops = ops; - - ret =nf_register_net_hooks (net, ops, num_ops); -if (ret !=0 ) - goto out_free; - -return ret; - -out_free: -__ip6t_unregister_table (net, new_table);return ret; }
net/netfilter/x_tables.c +42 -8
90 lines not on this page · raw message
Signature · 1 line
2.53.0
Florian Westphal
maintainerPATCH v2 3/8 netfilter: x_tables: add and use xt_unregister_table_pre_exit
In reply to Florian WestphalRemove the copypasted variants of _pre_exit and add one single function in the xtables core. ebtables is not compatible with x_tables and therefore unchanged.
This is a preparation patch to reduce noise in the followup bug fixes.
---
v2: no changes.
include/linux/netfilter/x_tables.h | 1 + include/linux/netfilter_arp/arp_tables.h | 1 - include/linux/netfilter_ipv4/ip_tables.h | 1 - include/linux/netfilter_ipv6/ip6_tables.h | 1 - net/ipv4/netfilter/arp_tables.c | 9 ------- net/ipv4/netfilter/arptable_filter.c | 2 +- net/ipv4/netfilter/ip_tables.c | 9 ------- net/ipv4/netfilter/iptable_filter.c | 2 +- net/ipv4/netfilter/iptable_mangle.c | 2 +- net/ipv4/netfilter/iptable_nat.c | 1 + net/ipv4/netfilter/iptable_raw.c | 2 +- net/ipv4/netfilter/iptable_security.c | 2 +- net/ipv6/netfilter/ip6_tables.c | 9 ------- net/ipv6/netfilter/ip6table_filter.c | 2 +- net/ipv6/netfilter/ip6table_mangle.c | 2 +- net/ipv6/netfilter/ip6table_nat.c | 1 + net/ipv6/netfilter/ip6table_raw.c | 2 +- net/ipv6/netfilter/ip6table_security.c | 2 +- net/netfilter/x_tables.c | 29 +++++++++++++++++++++++ 19 files changed, 41 insertions(+), 39 deletions(-)
include/linux/netfilter/x_tables.h +1 -0
@@ -309,6 +309,7 @@ struct xt_table *xt_register_table(struct net *net,struct xt_table_info *bootstrap, struct xt_table_info *newinfo);void *xt_unregister_table (struct xt_table *table); +void xt_unregister_table_pre_exit (struct net *net,u8 af,const char *name); struct xt_table_info *xt_replace_table(struct xt_table *table, unsigned int num_counters,
include/linux/netfilter_arp/arp_tables.h +0 -1
@@ -53,7 +53,6 @@ int arpt_register_table(struct net *net, const struct xt_table *table,const struct arpt_replace *repl, const struct nf_hook_ops *ops);void arpt_unregister_table (struct net *net,const char *name); -void arpt_unregister_table_pre_exit (struct net *net,const char *name);extern unsigned int arpt_do_table (void *priv,struct sk_buff *skb,const struct nf_hook_state *state);
include/linux/netfilter_ipv4/ip_tables.h +0 -1
@@ -26,7 +26,6 @@ int ipt_register_table(struct net *net, const struct xt_table *table,const struct ipt_replace *repl, const struct nf_hook_ops *ops); -void ipt_unregister_table_pre_exit (struct net *net,const char *name);void ipt_unregister_table_exit (struct net *net,const char *name);/* Standard entry. */
include/linux/netfilter_ipv6/ip6_tables.h +0 -1
@@ -27,7 +27,6 @@ extern void *ip6t_alloc_initial_table(const struct xt_table *);int ip6t_register_table (struct net *net,const struct xt_table *table,const struct ip6t_replace *repl,const struct nf_hook_ops *ops); -void ip6t_unregister_table_pre_exit (struct net *net,const char *name);void ip6t_unregister_table_exit (struct net *net,const char *name);extern unsigned int ip6t_do_table (void *priv,struct sk_buff *skb,const struct nf_hook_state *state);
net/ipv4/netfilter/arp_tables.c +0 -9
@@ -1554,15 +1554,6 @@ int arpt_register_table(struct net *net,return ret; } -void arpt_unregister_table_pre_exit (struct net *net,const char *name) -{ -struct xt_table *table =xt_find_table (net,NFPROTO_ARP , name); - -if (table) -nf_unregister_net_hooks (net, table->ops,hweight32 (table->valid_hooks)); -} -EXPORT_SYMBOL (arpt_unregister_table_pre_exit); -void arpt_unregister_table (struct net *net,const char *name) {struct xt_table *table =xt_find_table (net,NFPROTO_ARP , name);
net/ipv4/netfilter/arptable_filter.c +1 -1
@@ -43,7 +43,7 @@ static int arptable_filter_table_init(struct net *net) static void __net_exit arptable_filter_net_pre_exit(struct net *net) { -arpt_unregister_table_pre_exit (net,"filter" ); +xt_unregister_table_pre_exit (net,NFPROTO_ARP ,"filter" ); } static void __net_exit arptable_filter_net_exit(struct net *net)
net/ipv4/netfilter/ip_tables.c +0 -9
@@ -1756,14 +1756,6 @@ int ipt_register_table(struct net *net, const struct xt_table *table,return ret; } -void ipt_unregister_table_pre_exit (struct net *net,const char *name) -{ -struct xt_table *table =xt_find_table (net,NFPROTO_IPV4 , name); - -if (table) -nf_unregister_net_hooks (net, table->ops,hweight32 (table->valid_hooks)); -} -void ipt_unregister_table_exit (struct net *net,const char *name) {struct xt_table *table =xt_find_table (net,NFPROTO_IPV4 , name);
@@ -1854,7 +1846,6 @@ static void __exit ip_tables_fini(void) }EXPORT_SYMBOL (ipt_register_table); -EXPORT_SYMBOL (ipt_unregister_table_pre_exit);EXPORT_SYMBOL (ipt_unregister_table_exit);EXPORT_SYMBOL (ipt_do_table);module_init (ip_tables_init);
net/ipv4/netfilter/iptable_filter.c +1 -1
@@ -61,7 +61,7 @@ static int __net_init iptable_filter_net_init(struct net *net) static void __net_exit iptable_filter_net_pre_exit(struct net *net) { -ipt_unregister_table_pre_exit (net,"filter" ); +xt_unregister_table_pre_exit (net,NFPROTO_IPV4 ,"filter" ); } static void __net_exit iptable_filter_net_exit(struct net *net)
net/ipv4/netfilter/iptable_mangle.c +1 -1
@@ -96,7 +96,7 @@ static int iptable_mangle_table_init(struct net *net) static void __net_exit iptable_mangle_net_pre_exit(struct net *net) { -ipt_unregister_table_pre_exit (net,"mangle" ); +xt_unregister_table_pre_exit (net,NFPROTO_IPV4 ,"mangle" ); } static void __net_exit iptable_mangle_net_exit(struct net *net)
net/ipv4/netfilter/iptable_nat.c +1 -0
@@ -129,6 +129,7 @@ static int iptable_nat_table_init(struct net *net) static void __net_exit iptable_nat_net_pre_exit(struct net *net) {ipt_nat_unregister_lookups (net); +xt_unregister_table_pre_exit (net,NFPROTO_IPV4 ,"nat" ); } static void __net_exit iptable_nat_net_exit(struct net *net)
net/ipv4/netfilter/iptable_raw.c +1 -1
@@ -53,7 +53,7 @@ static int iptable_raw_table_init(struct net *net) static void __net_exit iptable_raw_net_pre_exit(struct net *net) { -ipt_unregister_table_pre_exit (net,"raw" ); +xt_unregister_table_pre_exit (net,NFPROTO_IPV4 ,"raw" ); } static void __net_exit iptable_raw_net_exit(struct net *net)
net/ipv4/netfilter/iptable_security.c +1 -1
@@ -50,7 +50,7 @@ static int iptable_security_table_init(struct net *net) static void __net_exit iptable_security_net_pre_exit(struct net *net) { -ipt_unregister_table_pre_exit (net,"security" ); +xt_unregister_table_pre_exit (net,NFPROTO_IPV4 ,"security" ); } static void __net_exit iptable_security_net_exit(struct net *net)
net/ipv6/netfilter/ip6_tables.c +0 -9
@@ -1765,14 +1765,6 @@ int ip6t_register_table(struct net *net, const struct xt_table *table,return ret; } -void ip6t_unregister_table_pre_exit (struct net *net,const char *name) -{ -struct xt_table *table =xt_find_table (net,NFPROTO_IPV6 , name); - -if (table) -nf_unregister_net_hooks (net, table->ops,hweight32 (table->valid_hooks)); -} -void ip6t_unregister_table_exit (struct net *net,const char *name) {struct xt_table *table =xt_find_table (net,NFPROTO_IPV6 , name);
@@ -1864,7 +1856,6 @@ static void __exit ip6_tables_fini(void) }EXPORT_SYMBOL (ip6t_register_table); -EXPORT_SYMBOL (ip6t_unregister_table_pre_exit);EXPORT_SYMBOL (ip6t_unregister_table_exit);EXPORT_SYMBOL (ip6t_do_table);
net/ipv6/netfilter/ip6table_filter.c +1 -1
@@ -60,7 +60,7 @@ static int __net_init ip6table_filter_net_init(struct net *net) static void __net_exit ip6table_filter_net_pre_exit(struct net *net) { -ip6t_unregister_table_pre_exit (net,"filter" ); +xt_unregister_table_pre_exit (net,NFPROTO_IPV6 ,"filter" ); } static void __net_exit ip6table_filter_net_exit(struct net *net)
net/ipv6/netfilter/ip6table_mangle.c +1 -1
@@ -89,7 +89,7 @@ static int ip6table_mangle_table_init(struct net *net) static void __net_exit ip6table_mangle_net_pre_exit(struct net *net) { -ip6t_unregister_table_pre_exit (net,"mangle" ); +xt_unregister_table_pre_exit (net,NFPROTO_IPV6 ,"mangle" ); } static void __net_exit ip6table_mangle_net_exit(struct net *net)
net/ipv6/netfilter/ip6table_nat.c +1 -0
@@ -131,6 +131,7 @@ static int ip6table_nat_table_init(struct net *net) static void __net_exit ip6table_nat_net_pre_exit(struct net *net) {ip6t_nat_unregister_lookups (net); +xt_unregister_table_pre_exit (net,NFPROTO_IPV6 ,"nat" ); } static void __net_exit ip6table_nat_net_exit(struct net *net)
net/ipv6/netfilter/ip6table_raw.c +1 -1
@@ -52,7 +52,7 @@ static int ip6table_raw_table_init(struct net *net) static void __net_exit ip6table_raw_net_pre_exit(struct net *net) { -ip6t_unregister_table_pre_exit (net,"raw" ); +xt_unregister_table_pre_exit (net,NFPROTO_IPV6 ,"raw" ); } static void __net_exit ip6table_raw_net_exit(struct net *net)
net/ipv6/netfilter/ip6table_security.c +1 -1
@@ -49,7 +49,7 @@ static int ip6table_security_table_init(struct net *net) static void __net_exit ip6table_security_net_pre_exit(struct net *net) { -ip6t_unregister_table_pre_exit (net,"security" ); +xt_unregister_table_pre_exit (net,NFPROTO_IPV6 ,"security" ); } static void __net_exit ip6table_security_net_exit(struct net *net)
net/netfilter/x_tables.c +29 -0
@@ -1650,6 +1650,35 @@ void *xt_unregister_table(struct xt_table *table)return private; }EXPORT_SYMBOL_GPL (xt_unregister_table); + +/** +* xt_unregister_table_pre_exit - pre-shutdown unregister of a table +* @net: network namespace +* @af: address family (e.g., NFPROTO_IPV4, NFPROTO_IPV6) +* @name: name of the table to unregister +* +* Unregisters the specified netfilter table from the given network namespace +* and also unregisters the hooks from netfilter core: no new packets will be +* processed. +*/ +void xt_unregister_table_pre_exit (struct net *net,u8 af,const char *name) +{ +struct xt_pernet *xt_net =net_generic (net, xt_pernet_id); +struct xt_table *t; + +mutex_lock (&xt[af].mutex); +list_for_each_entry (t, &xt_net->tables[af], list) { +if (strcmp (t->name, name) ==0 ) { +mutex_unlock (&xt[af].mutex); + +if (t->ops)/* nat table registers with nat core, t->ops is NULL. */ +nf_unregister_net_hooks (net, t->ops,hweight32 (t->valid_hooks)); +return ; + } + } +mutex_unlock (&xt[af].mutex); +} +EXPORT_SYMBOL (xt_unregister_table_pre_exit);#endif #ifdef CONFIG_PROC_FS
Signature · 1 line
2.53.0
Florian Westphal
maintainerPATCH v2 4/8 netfilter: x_tables: unregister the templates first
In reply to Florian WestphalWhen the module is going away we need to zap the template first. Else there is a small race window where userspace could instantiate a new table after the pernet exit function has removed the current table.
---
v2: no changes
net/ipv4/netfilter/arptable_filter.c | 2 +- net/ipv4/netfilter/iptable_filter.c | 2 +- net/ipv4/netfilter/iptable_mangle.c | 2 +- net/ipv4/netfilter/iptable_raw.c | 2 +- net/ipv4/netfilter/iptable_security.c | 2 +- net/ipv6/netfilter/ip6table_filter.c | 2 +- net/ipv6/netfilter/ip6table_mangle.c | 2 +- net/ipv6/netfilter/ip6table_raw.c | 2 +- net/ipv6/netfilter/ip6table_security.c | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-)
net/ipv4/netfilter/arptable_filter.c +1 -1
@@ -82,8 +82,8 @@ static int __init arptable_filter_init(void) static void __exit arptable_filter_fini(void) { -unregister_pernet_subsys (&arptable_filter_net_ops);xt_unregister_template (&packet_filter); +unregister_pernet_subsys (&arptable_filter_net_ops);kfree (arpfilter_ops); }
net/ipv4/netfilter/iptable_filter.c +1 -1
@@ -101,8 +101,8 @@ static int __init iptable_filter_init(void) static void __exit iptable_filter_fini(void) { -unregister_pernet_subsys (&iptable_filter_net_ops);xt_unregister_template (&packet_filter); +unregister_pernet_subsys (&iptable_filter_net_ops);kfree (filter_ops); }
net/ipv4/netfilter/iptable_mangle.c +1 -1
@@ -135,8 +135,8 @@ static int __init iptable_mangle_init(void) static void __exit iptable_mangle_fini(void) { -unregister_pernet_subsys (&iptable_mangle_net_ops);xt_unregister_template (&packet_mangler); +unregister_pernet_subsys (&iptable_mangle_net_ops);kfree (mangle_ops); }
net/ipv4/netfilter/iptable_raw.c +1 -1
@@ -100,9 +100,9 @@ static int __init iptable_raw_init(void) static void __exit iptable_raw_fini(void) { +xt_unregister_template (&packet_raw);unregister_pernet_subsys (&iptable_raw_net_ops);kfree (rawtable_ops); -xt_unregister_template (&packet_raw); }module_init (iptable_raw_init);
net/ipv4/netfilter/iptable_security.c +1 -1
@@ -89,9 +89,9 @@ static int __init iptable_security_init(void) static void __exit iptable_security_fini(void) { +xt_unregister_template (&security_table);unregister_pernet_subsys (&iptable_security_net_ops);kfree (sectbl_ops); -xt_unregister_template (&security_table); }module_init (iptable_security_init);
net/ipv6/netfilter/ip6table_filter.c +1 -1
@@ -100,8 +100,8 @@ static int __init ip6table_filter_init(void) static void __exit ip6table_filter_fini(void) { -unregister_pernet_subsys (&ip6table_filter_net_ops);xt_unregister_template (&packet_filter); +unregister_pernet_subsys (&ip6table_filter_net_ops);kfree (filter_ops); }
net/ipv6/netfilter/ip6table_mangle.c +1 -1
@@ -128,8 +128,8 @@ static int __init ip6table_mangle_init(void) static void __exit ip6table_mangle_fini(void) { -unregister_pernet_subsys (&ip6table_mangle_net_ops);xt_unregister_template (&packet_mangler); +unregister_pernet_subsys (&ip6table_mangle_net_ops);kfree (mangle_ops); }
net/ipv6/netfilter/ip6table_raw.c +1 -1
@@ -98,8 +98,8 @@ static int __init ip6table_raw_init(void) static void __exit ip6table_raw_fini(void) { -unregister_pernet_subsys (&ip6table_raw_net_ops);xt_unregister_template (&packet_raw); +unregister_pernet_subsys (&ip6table_raw_net_ops);kfree (rawtable_ops); }
net/ipv6/netfilter/ip6table_security.c +1 -1
@@ -88,8 +88,8 @@ static int __init ip6table_security_init(void) static void __exit ip6table_security_fini(void) { -unregister_pernet_subsys (&ip6table_security_net_ops);xt_unregister_template (&security_table); +unregister_pernet_subsys (&ip6table_security_net_ops);kfree (sectbl_ops); }
Signature · 1 line
2.53.0
Florian Westphal
maintainerPATCH v2 5/8 netfilter: x_tables: add and use xtables_unregister_table_exit
In reply to Florian WestphalPrevious change added xtables_unregister_table_pre_exit to detach the table from the packetpath and to unlink it from the active table list. In case of rmmod, userspace that is doing set/getsockopt for this table will not be able to re-instantiate the table:
1. The larval table has been removed already
2. existing instantiated table is no longer on the xt pernet table list.
This adds the second stage helper:
unlink the table from the dying list, free the hook ops (if any) and do the audit notification. It replaces xt_unregister_table().
---
v2: fix nat tables to call xt_unregister_table_pre_exit+synchronize_rcu on error.
include/linux/netfilter/x_tables.h | 2 +- net/ipv4/netfilter/arp_tables.c | 9 ++-- net/ipv4/netfilter/ip_tables.c | 9 ++-- net/ipv4/netfilter/iptable_nat.c | 5 +- net/ipv6/netfilter/ip6_tables.c | 9 ++-- net/ipv6/netfilter/ip6table_nat.c | 5 +- net/netfilter/x_tables.c | 81 +++++++++++++++++++++++------- 7 files changed, 83 insertions(+), 37 deletions(-)
include/linux/netfilter/x_tables.h +1 -1
@@ -308,8 +308,8 @@ struct xt_table *xt_register_table(struct net *net,const struct nf_hook_ops *template_ops, struct xt_table_info *bootstrap, struct xt_table_info *newinfo); -void *xt_unregister_table (struct xt_table *table);void xt_unregister_table_pre_exit (struct net *net,u8 af,const char *name); +struct xt_table *xt_unregister_table_exit (struct net *net,u8 af,const char *name); struct xt_table_info *xt_replace_table(struct xt_table *table, unsigned int num_counters,
net/ipv4/netfilter/arp_tables.c +4 -5
@@ -1501,13 +1501,11 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *lenstatic void __arpt_unregister_table (struct net *net,struct xt_table *table) { -struct xt_table_info *private; -void *loc_cpu_entry; +struct xt_table_info *private = table->private;struct module *table_owner = table->me; +void *loc_cpu_entry;struct arpt_entry *iter; - private =xt_unregister_table (table); -/* Decrease module usage counts and free resources */ loc_cpu_entry = private->entries;xt_entry_foreach (iter, loc_cpu_entry, private->size)
@@ -1515,6 +1513,7 @@ static void __arpt_unregister_table(struct net *net, struct xt_table *table)if (private->number > private->initial_entries)module_put (table_owner);xt_free_table_info (private); +kfree (table); } int arpt_register_table(struct net *net,
@@ -1556,7 +1555,7 @@ int arpt_register_table(struct net *net,void arpt_unregister_table (struct net *net,const char *name) { -struct xt_table *table =xt_find_table (net,NFPROTO_ARP , name); +struct xt_table *table =xt_unregister_table_exit (net,NFPROTO_ARP , name);if (table)__arpt_unregister_table (net, table);
net/ipv4/netfilter/ip_tables.c +4 -5
@@ -1704,12 +1704,10 @@ do_ipt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)static void __ipt_unregister_table (struct net *net,struct xt_table *table) { -struct xt_table_info *private; -void *loc_cpu_entry; +struct xt_table_info *private = table->private;struct module *table_owner = table->me;struct ipt_entry *iter; - - private =xt_unregister_table (table); +void *loc_cpu_entry;/* Decrease module usage counts and free resources */ loc_cpu_entry = private->entries;
@@ -1718,6 +1716,7 @@ static void __ipt_unregister_table(struct net *net, struct xt_table *table)if (private->number > private->initial_entries)module_put (table_owner);xt_free_table_info (private); +kfree (table); } int ipt_register_table(struct net *net, const struct xt_table *table,
@@ -1758,7 +1757,7 @@ int ipt_register_table(struct net *net, const struct xt_table *table,void ipt_unregister_table_exit (struct net *net,const char *name) { -struct xt_table *table =xt_find_table (net,NFPROTO_IPV4 , name); +struct xt_table *table =xt_unregister_table_exit (net,NFPROTO_IPV4 , name);if (table)__ipt_unregister_table (net, table);
net/ipv4/netfilter/iptable_nat.c +4 -1
@@ -119,8 +119,11 @@ static int iptable_nat_table_init(struct net *net) } ret =ipt_nat_register_lookups (net); -if (ret <0 ) +if (ret <0 ) { +xt_unregister_table_pre_exit (net,NFPROTO_IPV4 ,"nat" ); +synchronize_rcu ();ipt_unregister_table_exit (net,"nat" ); + }kfree (repl);return ret;
net/ipv6/netfilter/ip6_tables.c +4 -5
@@ -1713,12 +1713,10 @@ do_ip6t_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)static void __ip6t_unregister_table (struct net *net,struct xt_table *table) { -struct xt_table_info *private; -void *loc_cpu_entry; +struct xt_table_info *private = table->private;struct module *table_owner = table->me;struct ip6t_entry *iter; - - private =xt_unregister_table (table); +void *loc_cpu_entry;/* Decrease module usage counts and free resources */ loc_cpu_entry = private->entries;
@@ -1727,6 +1725,7 @@ static void __ip6t_unregister_table(struct net *net, struct xt_table *table)if (private->number > private->initial_entries)module_put (table_owner);xt_free_table_info (private); +kfree (table); } int ip6t_register_table(struct net *net, const struct xt_table *table,
@@ -1767,7 +1766,7 @@ int ip6t_register_table(struct net *net, const struct xt_table *table,void ip6t_unregister_table_exit (struct net *net,const char *name) { -struct xt_table *table =xt_find_table (net,NFPROTO_IPV6 , name); +struct xt_table *table =xt_unregister_table_exit (net,NFPROTO_IPV6 , name);if (table)__ip6t_unregister_table (net, table);
net/ipv6/netfilter/ip6table_nat.c +4 -1
@@ -121,8 +121,11 @@ static int ip6table_nat_table_init(struct net *net) } ret =ip6t_nat_register_lookups (net); -if (ret <0 ) +if (ret <0 ) { +xt_unregister_table_pre_exit (net,NFPROTO_IPV6 ,"nat" ); +synchronize_rcu ();ip6t_unregister_table_exit (net,"nat" ); + }kfree (repl);return ret;
net/netfilter/x_tables.c +62 -19
125 lines not on this page · raw message
Signature · 1 line
2.53.0
Florian Westphal
maintainerPATCH v2 6/8 netfilter: ebtables: move to two-stage removal scheme
In reply to Florian WestphalLike previous patches for x_tables, follow same pattern in ebtables. We can't reuse xt helpers: ebt_table struct layout is incompatible.
table->ops assignment is now done while still holding the ebt mutex to make sure we never expose partially-filled table struct.
---
v2: place synchronize_rcu after hook register failure.
net/bridge/netfilter/ebtable_broute.c | 2 +- net/bridge/netfilter/ebtable_nat.c | 2 +- net/bridge/netfilter/ebtables.c | 60 +++++++++++++++++---------- 3 files changed, 39 insertions(+), 25 deletions(-)
net/bridge/netfilter/ebtable_broute.c +1 -1
@@ -128,8 +128,8 @@ static int __init ebtable_broute_init(void) static void __exit ebtable_broute_fini(void) { -unregister_pernet_subsys (&broute_net_ops);ebt_unregister_template (&broute_table); +unregister_pernet_subsys (&broute_net_ops); }module_init (ebtable_broute_init);
net/bridge/netfilter/ebtable_nat.c +1 -1
@@ -109,8 +109,8 @@ static int __init ebtable_nat_init(void) static void __exit ebtable_nat_fini(void) { -unregister_pernet_subsys (&frame_nat_net_ops);ebt_unregister_template (&frame_nat); +unregister_pernet_subsys (&frame_nat_net_ops); }module_init (ebtable_nat_init);
net/bridge/netfilter/ebtables.c +37 -23
115 lines not on this page · raw message
Signature · 1 line
2.53.0
Florian Westphal
maintainerPATCH v2 7/8 netfilter: ebtables: close dangling table module init race
In reply to Florian Westphalsashiko reported for a related patch:
In modules like iptable_raw.c, [..], if register_pernet_subsys() fails, the rollback might call kfree(rawtable_ops) before [..]
During this window, could a concurrent userspace process find the globally visible template, trigger table_init(), [..]
The table init functions must always register the template last.
Otherwise, set/getsockopt can instantiate a table in a namespace while the required pernet ops (contain the destructor) isn't available. This change is also required in x_tables, handled in followup change.
---
v2: also handle __init: register template last.
net/bridge/netfilter/ebtable_broute.c | 12 +++++------- net/bridge/netfilter/ebtable_filter.c | 12 +++++------- net/bridge/netfilter/ebtable_nat.c | 10 ++++------ 3 files changed, 14 insertions(+), 20 deletions(-)
net/bridge/netfilter/ebtable_broute.c +5 -7
@@ -112,18 +112,16 @@ static struct pernet_operations broute_net_ops = {static int __init ebtable_broute_init (void ) { -int ret =ebt_register_template (&broute_table, broute_table_init); +int ret =register_pernet_subsys (&broute_net_ops);if (ret)return ret; - ret =register_pernet_subsys (&broute_net_ops); -if (ret) { -ebt_unregister_template (&broute_table); -return ret; - } + ret =ebt_register_template (&broute_table, broute_table_init); +if (ret) +unregister_pernet_subsys (&broute_net_ops); -return 0 ; +return ret; } static void __exit ebtable_broute_fini(void)
net/bridge/netfilter/ebtable_filter.c +5 -7
@@ -93,18 +93,16 @@ static struct pernet_operations frame_filter_net_ops = {static int __init ebtable_filter_init (void ) { -int ret =ebt_register_template (&frame_filter, frame_filter_table_init); +int ret =register_pernet_subsys (&frame_filter_net_ops);if (ret)return ret; - ret =register_pernet_subsys (&frame_filter_net_ops); -if (ret) { -ebt_unregister_template (&frame_filter); -return ret; - } + ret =ebt_register_template (&frame_filter, frame_filter_table_init); +if (ret) +unregister_pernet_subsys (&frame_filter_net_ops); -return 0 ; +return ret; } static void __exit ebtable_filter_fini(void)
net/bridge/netfilter/ebtable_nat.c +4 -6
@@ -93,16 +93,14 @@ static struct pernet_operations frame_nat_net_ops = {static int __init ebtable_nat_init (void ) { -int ret =ebt_register_template (&frame_nat, frame_nat_table_init); +int ret =register_pernet_subsys (&frame_nat_net_ops);if (ret)return ret; - ret =register_pernet_subsys (&frame_nat_net_ops); -if (ret) { -ebt_unregister_template (&frame_nat); -return ret; - } + ret =ebt_register_template (&frame_nat, frame_nat_table_init); +if (ret) +unregister_pernet_subsys (&frame_nat_net_ops);return ret; }
Signature · 1 line
2.53.0
Florian Westphal
maintainerPATCH v2 8/8 netfilter: x_tables: close dangling table module init race
In reply to Florian WestphalSimilar to the previous ebtables patch:
template add exposes the table to userspace, we must do this last to rnsure the pernet ops are set up (contain the destructors).
---
v2: new in this series.
net/ipv4/netfilter/arptable_filter.c | 23 ++++++++++++----------- net/ipv4/netfilter/iptable_filter.c | 23 ++++++++++++----------- net/ipv4/netfilter/iptable_mangle.c | 25 +++++++++++++------------ net/ipv4/netfilter/iptable_raw.c | 22 +++++++++++----------- net/ipv4/netfilter/iptable_security.c | 19 +++++++++++-------- net/ipv6/netfilter/ip6table_filter.c | 22 +++++++++++----------- net/ipv6/netfilter/ip6table_mangle.c | 23 ++++++++++++----------- net/ipv6/netfilter/ip6table_raw.c | 20 ++++++++++---------- net/ipv6/netfilter/ip6table_security.c | 23 ++++++++++++----------- 9 files changed, 104 insertions(+), 96 deletions(-)
net/ipv4/netfilter/arptable_filter.c +12 -11
@@ -58,25 +58,26 @@ static struct pernet_operations arptable_filter_net_ops = {static int __init arptable_filter_init (void ) { -int ret =xt_register_template (&packet_filter, - arptable_filter_table_init); - -if (ret <0 ) -return ret; +int ret; arpfilter_ops =xt_hook_ops_alloc (&packet_filter, arpt_do_table); -if (IS_ERR (arpfilter_ops)) { -xt_unregister_template (&packet_filter); +if (IS_ERR (arpfilter_ops))return PTR_ERR (arpfilter_ops); - } ret =register_pernet_subsys (&arptable_filter_net_ops); +if (ret <0 ) + goto err_free; + + ret =xt_register_template (&packet_filter, + arptable_filter_table_init);if (ret <0 ) { -xt_unregister_template (&packet_filter); -kfree (arpfilter_ops); -return ret; +unregister_pernet_subsys (&arptable_filter_net_ops); + goto err_free; } +return 0 ; +err_free: +kfree (arpfilter_ops);return ret; }
net/ipv4/netfilter/iptable_filter.c +12 -11
@@ -77,26 +77,27 @@ static struct pernet_operations iptable_filter_net_ops = {static int __init iptable_filter_init (void ) { -int ret =xt_register_template (&packet_filter, - iptable_filter_table_init); - -if (ret <0 ) -return ret; +int ret; filter_ops =xt_hook_ops_alloc (&packet_filter, ipt_do_table); -if (IS_ERR (filter_ops)) { -xt_unregister_template (&packet_filter); +if (IS_ERR (filter_ops))return PTR_ERR (filter_ops); - } ret =register_pernet_subsys (&iptable_filter_net_ops); +if (ret <0 ) + goto err_free; + + ret =xt_register_template (&packet_filter, + iptable_filter_table_init);if (ret <0 ) { -xt_unregister_template (&packet_filter); -kfree (filter_ops); -return ret; +unregister_pernet_subsys (&iptable_filter_net_ops); + goto err_free; }return 0 ; +err_free: +kfree (filter_ops); +return ret; } static void __exit iptable_filter_fini(void)
net/ipv4/netfilter/iptable_mangle.c +13 -12
@@ -111,25 +111,26 @@ static struct pernet_operations iptable_mangle_net_ops = {static int __init iptable_mangle_init (void ) { -int ret =xt_register_template (&packet_mangler, - iptable_mangle_table_init); -if (ret <0 ) -return ret; +int ret; mangle_ops =xt_hook_ops_alloc (&packet_mangler, iptable_mangle_hook); -if (IS_ERR (mangle_ops)) { -xt_unregister_template (&packet_mangler); - ret =PTR_ERR (mangle_ops); -return ret; - } +if (IS_ERR (mangle_ops)) +return PTR_ERR (mangle_ops); ret =register_pernet_subsys (&iptable_mangle_net_ops); +if (ret <0 ) + goto err_free; + + ret =xt_register_template (&packet_mangler, + iptable_mangle_table_init);if (ret <0 ) { -xt_unregister_template (&packet_mangler); -kfree (mangle_ops); -return ret; +unregister_pernet_subsys (&iptable_mangle_net_ops); + goto err_free; } +return 0 ; +err_free: +kfree (mangle_ops);return ret; }
net/ipv4/netfilter/iptable_raw.c +11 -11
@@ -77,24 +77,24 @@ static int __init iptable_raw_init(void)pr_info ("Enabling raw table before defrag\n" ); } - ret =xt_register_template (table, - iptable_raw_table_init); -if (ret <0 ) -return ret; - rawtable_ops =xt_hook_ops_alloc (table, ipt_do_table); -if (IS_ERR (rawtable_ops)) { -xt_unregister_template (table); +if (IS_ERR (rawtable_ops))return PTR_ERR (rawtable_ops); - } ret =register_pernet_subsys (&iptable_raw_net_ops); +if (ret <0 ) + goto err_free; + + ret =xt_register_template (table, + iptable_raw_table_init);if (ret <0 ) { -xt_unregister_template (table); -kfree (rawtable_ops); -return ret; +unregister_pernet_subsys (&iptable_raw_net_ops); + goto err_free; } +return 0 ; +err_free: +kfree (rawtable_ops);return ret; }
net/ipv4/netfilter/iptable_security.c +11 -8
@@ -65,11 +65,7 @@ static struct pernet_operations iptable_security_net_ops = {static int __init iptable_security_init (void ) { -int ret =xt_register_template (&security_table, - iptable_security_table_init); - -if (ret <0 ) -return ret; +int ret; sectbl_ops =xt_hook_ops_alloc (&security_table, ipt_do_table);if (IS_ERR (sectbl_ops)) {
@@ -78,12 +74,19 @@ static int __init iptable_security_init(void) } ret =register_pernet_subsys (&iptable_security_net_ops); +if (ret <0 ) + goto err_free; + + ret =xt_register_template (&security_table, + iptable_security_table_init);if (ret <0 ) { -xt_unregister_template (&security_table); -kfree (sectbl_ops); -return ret; +unregister_pernet_subsys (&iptable_security_net_ops); + goto err_free; } +return 0 ; +err_free: +kfree (sectbl_ops);return ret; }
net/ipv6/netfilter/ip6table_filter.c +11 -11
@@ -76,25 +76,25 @@ static struct pernet_operations ip6table_filter_net_ops = {static int __init ip6table_filter_init (void ) { -int ret =xt_register_template (&packet_filter, - ip6table_filter_table_init); - -if (ret <0 ) -return ret; +int ret; filter_ops =xt_hook_ops_alloc (&packet_filter, ip6t_do_table); -if (IS_ERR (filter_ops)) { -xt_unregister_template (&packet_filter); +if (IS_ERR (filter_ops))return PTR_ERR (filter_ops); - } ret =register_pernet_subsys (&ip6table_filter_net_ops); +if (ret <0 ) + goto err_free; + + ret =xt_register_template (&packet_filter, ip6table_filter_table_init);if (ret <0 ) { -xt_unregister_template (&packet_filter); -kfree (filter_ops); -return ret; +unregister_pernet_subsys (&ip6table_filter_net_ops); + goto err_free; } +return 0 ; +err_free: +kfree (filter_ops);return ret; }
net/ipv6/netfilter/ip6table_mangle.c +12 -11
@@ -104,25 +104,26 @@ static struct pernet_operations ip6table_mangle_net_ops = {static int __init ip6table_mangle_init (void ) { -int ret =xt_register_template (&packet_mangler, - ip6table_mangle_table_init); - -if (ret <0 ) -return ret; +int ret; mangle_ops =xt_hook_ops_alloc (&packet_mangler, ip6table_mangle_hook); -if (IS_ERR (mangle_ops)) { -xt_unregister_template (&packet_mangler); +if (IS_ERR (mangle_ops))return PTR_ERR (mangle_ops); - } ret =register_pernet_subsys (&ip6table_mangle_net_ops); +if (ret <0 ) + goto err_free; + + ret =xt_register_template (&packet_mangler, + ip6table_mangle_table_init);if (ret <0 ) { -xt_unregister_template (&packet_mangler); -kfree (mangle_ops); -return ret; +unregister_pernet_subsys (&ip6table_mangle_net_ops); + goto err_free; } +return 0 ; +err_free: +kfree (mangle_ops);return ret; }
net/ipv6/netfilter/ip6table_raw.c +10 -10
@@ -75,24 +75,24 @@ static int __init ip6table_raw_init(void)pr_info ("Enabling raw table before defrag\n" ); } - ret =xt_register_template (table, ip6table_raw_table_init); -if (ret <0 ) -return ret; -/* Register hooks */ rawtable_ops =xt_hook_ops_alloc (table, ip6t_do_table); -if (IS_ERR (rawtable_ops)) { -xt_unregister_template (table); +if (IS_ERR (rawtable_ops))return PTR_ERR (rawtable_ops); - } ret =register_pernet_subsys (&ip6table_raw_net_ops); +if (ret <0 ) + goto err_free; + + ret =xt_register_template (table, ip6table_raw_table_init);if (ret <0 ) { -kfree (rawtable_ops); -xt_unregister_template (table); -return ret; +unregister_pernet_subsys (&ip6table_raw_net_ops); + goto err_free; } +return 0 ; +err_free: +kfree (rawtable_ops);return ret; }
net/ipv6/netfilter/ip6table_security.c +12 -11
@@ -64,25 +64,26 @@ static struct pernet_operations ip6table_security_net_ops = {static int __init ip6table_security_init (void ) { -int ret =xt_register_template (&security_table, - ip6table_security_table_init); - -if (ret <0 ) -return ret; +int ret; sectbl_ops =xt_hook_ops_alloc (&security_table, ip6t_do_table); -if (IS_ERR (sectbl_ops)) { -xt_unregister_template (&security_table); +if (IS_ERR (sectbl_ops))return PTR_ERR (sectbl_ops); - } ret =register_pernet_subsys (&ip6table_security_net_ops); +if (ret <0 ) + goto err_free; + + ret =xt_register_template (&security_table, + ip6table_security_table_init);if (ret <0 ) { -kfree (sectbl_ops); -xt_unregister_template (&security_table); -return ret; +unregister_pernet_subsys (&ip6table_security_net_ops); + goto err_free; } +return 0 ; +err_free: +kfree (sectbl_ops);return ret; }
Signature · 1 line
2.53.0