mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-05-16 02:01:18 -04:00
netfilter: x_tables: add and use xtables_unregister_table_exit
Previous 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().
Fixes: fdacd57c79 ("netfilter: x_tables: never register tables by default")
Reported-by: Tristan Madani <tristan@talencesecurity.com>
Reviewed-by: Tristan Madani <tristan@talencesecurity.com>
Closes: https://lore.kernel.org/netfilter-devel/20260429175613.1459342-1-tristmd@gmail.com/
Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
This commit is contained in:
committed by
Pablo Neira Ayuso
parent
d338693d77
commit
b4597d5fd7
@@ -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,
|
||||
|
||||
@@ -1501,13 +1501,11 @@ static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len
|
||||
|
||||
static 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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -55,6 +55,9 @@ static struct list_head xt_templates[NFPROTO_NUMPROTO];
|
||||
|
||||
struct xt_pernet {
|
||||
struct list_head tables[NFPROTO_NUMPROTO];
|
||||
|
||||
/* stash area used during netns exit */
|
||||
struct list_head dead_tables[NFPROTO_NUMPROTO];
|
||||
};
|
||||
|
||||
struct compat_delta {
|
||||
@@ -1634,23 +1637,6 @@ struct xt_table *xt_register_table(struct net *net,
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(xt_register_table);
|
||||
|
||||
void *xt_unregister_table(struct xt_table *table)
|
||||
{
|
||||
struct xt_table_info *private;
|
||||
|
||||
mutex_lock(&xt[table->af].mutex);
|
||||
private = table->private;
|
||||
list_del(&table->list);
|
||||
mutex_unlock(&xt[table->af].mutex);
|
||||
audit_log_nfcfg(table->name, table->af, private->number,
|
||||
AUDIT_XT_OP_UNREGISTER, GFP_KERNEL);
|
||||
kfree(table->ops);
|
||||
kfree(table);
|
||||
|
||||
return private;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(xt_unregister_table);
|
||||
|
||||
/**
|
||||
* xt_unregister_table_pre_exit - pre-shutdown unregister of a table
|
||||
* @net: network namespace
|
||||
@@ -1660,6 +1646,14 @@ EXPORT_SYMBOL_GPL(xt_unregister_table);
|
||||
* Unregisters the specified netfilter table from the given network namespace
|
||||
* and also unregisters the hooks from netfilter core: no new packets will be
|
||||
* processed.
|
||||
*
|
||||
* This must be called prior to xt_unregister_table_exit() from the pernet
|
||||
* .pre_exit callback. After this call, the table is no longer visible to
|
||||
* the get/setsockopt path. In case of rmmod, module exit path must have
|
||||
* called xt_unregister_template() prior to unregistering pernet ops to
|
||||
* prevent re-instantiation of the table.
|
||||
*
|
||||
* See also: xt_unregister_table_exit()
|
||||
*/
|
||||
void xt_unregister_table_pre_exit(struct net *net, u8 af, const char *name)
|
||||
{
|
||||
@@ -1669,6 +1663,7 @@ void xt_unregister_table_pre_exit(struct net *net, u8 af, const char *name)
|
||||
mutex_lock(&xt[af].mutex);
|
||||
list_for_each_entry(t, &xt_net->tables[af], list) {
|
||||
if (strcmp(t->name, name) == 0) {
|
||||
list_move(&t->list, &xt_net->dead_tables[af]);
|
||||
mutex_unlock(&xt[af].mutex);
|
||||
|
||||
if (t->ops) /* nat table registers with nat core, t->ops is NULL. */
|
||||
@@ -1679,6 +1674,50 @@ void xt_unregister_table_pre_exit(struct net *net, u8 af, const char *name)
|
||||
mutex_unlock(&xt[af].mutex);
|
||||
}
|
||||
EXPORT_SYMBOL(xt_unregister_table_pre_exit);
|
||||
|
||||
/**
|
||||
* xt_unregister_table_exit - remove a table during namespace teardown
|
||||
* @net: the network namespace from which to unregister the table
|
||||
* @af: address family (e.g., NFPROTO_IPV4, NFPROTO_IPV6)
|
||||
* @name: name of the table to unregister
|
||||
*
|
||||
* Completes the unregister process for a table. This must be called from
|
||||
* the pernet ops .exit callback. This is the second stage after
|
||||
* xt_unregister_table_pre_exit().
|
||||
*
|
||||
* pair with xt_unregister_table_pre_exit() during namespace shutdown.
|
||||
*
|
||||
* Return: the unregistered table or NULL if the table was never
|
||||
* instantiated. The caller needs to kfree() the table after it
|
||||
* has removed the family specific matches/targets.
|
||||
*/
|
||||
struct xt_table *xt_unregister_table_exit(struct net *net, u8 af, const char *name)
|
||||
{
|
||||
struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
|
||||
struct xt_table *table;
|
||||
|
||||
mutex_lock(&xt[af].mutex);
|
||||
list_for_each_entry(table, &xt_net->dead_tables[af], list) {
|
||||
struct nf_hook_ops *ops = NULL;
|
||||
|
||||
if (strcmp(table->name, name) != 0)
|
||||
continue;
|
||||
|
||||
list_del(&table->list);
|
||||
|
||||
audit_log_nfcfg(table->name, table->af, table->private->number,
|
||||
AUDIT_XT_OP_UNREGISTER, GFP_KERNEL);
|
||||
swap(table->ops, ops);
|
||||
mutex_unlock(&xt[af].mutex);
|
||||
|
||||
kfree(ops);
|
||||
return table;
|
||||
}
|
||||
mutex_unlock(&xt[af].mutex);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
EXPORT_SYMBOL_GPL(xt_unregister_table_exit);
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_PROC_FS
|
||||
@@ -2125,8 +2164,10 @@ static int __net_init xt_net_init(struct net *net)
|
||||
struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NFPROTO_NUMPROTO; i++)
|
||||
for (i = 0; i < NFPROTO_NUMPROTO; i++) {
|
||||
INIT_LIST_HEAD(&xt_net->tables[i]);
|
||||
INIT_LIST_HEAD(&xt_net->dead_tables[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2135,8 +2176,10 @@ static void __net_exit xt_net_exit(struct net *net)
|
||||
struct xt_pernet *xt_net = net_generic(net, xt_pernet_id);
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NFPROTO_NUMPROTO; i++)
|
||||
for (i = 0; i < NFPROTO_NUMPROTO; i++) {
|
||||
WARN_ON_ONCE(!list_empty(&xt_net->tables[i]));
|
||||
WARN_ON_ONCE(!list_empty(&xt_net->dead_tables[i]));
|
||||
}
|
||||
}
|
||||
|
||||
static struct pernet_operations xt_net_ops = {
|
||||
|
||||
Reference in New Issue
Block a user