Commit 7181ebaf authored by Pablo Neira Ayuso's avatar Pablo Neira Ayuso

netfilter: fix possible removal of wrong hook

nf_unregister_net_hook() uses the nf_hook_ops fields as tuple to look up for
the corresponding hook in the list. However, we may have two hooks with exactly
the same configuration.

This shouldn't be a problem for nftables since every new chain has an unique
priv field set, but this may still cause us problems in the future, so better
address this problem now by keeping a reference to the original nf_hook_ops
structure to make sure we delete the right hook from nf_unregister_net_hook().

Fixes: 085db2c0 ("netfilter: Per network namespace netfilter hooks.")
Signed-off-by: default avatarPablo Neira Ayuso <pablo@netfilter.org>
Acked-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
parent 2385eb0c
...@@ -78,26 +78,27 @@ static struct list_head *find_nf_hook_list(struct net *net, ...@@ -78,26 +78,27 @@ static struct list_head *find_nf_hook_list(struct net *net,
return nf_hook_list; return nf_hook_list;
} }
struct nf_hook_entry {
const struct nf_hook_ops *orig_ops;
struct nf_hook_ops ops;
};
int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)
{ {
struct list_head *nf_hook_list; struct list_head *nf_hook_list;
struct nf_hook_ops *elem, *new; struct nf_hook_entry *entry;
struct nf_hook_ops *elem;
new = kzalloc(sizeof(*new), GFP_KERNEL); entry = kmalloc(sizeof(*entry), GFP_KERNEL);
if (!new) if (!entry)
return -ENOMEM; return -ENOMEM;
new->hook = reg->hook; entry->orig_ops = reg;
new->dev = reg->dev; entry->ops = *reg;
new->owner = reg->owner;
new->priv = reg->priv;
new->pf = reg->pf;
new->hooknum = reg->hooknum;
new->priority = reg->priority;
nf_hook_list = find_nf_hook_list(net, reg); nf_hook_list = find_nf_hook_list(net, reg);
if (!nf_hook_list) { if (!nf_hook_list) {
kfree(new); kfree(entry);
return -ENOENT; return -ENOENT;
} }
...@@ -106,7 +107,7 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg) ...@@ -106,7 +107,7 @@ int nf_register_net_hook(struct net *net, const struct nf_hook_ops *reg)
if (reg->priority < elem->priority) if (reg->priority < elem->priority)
break; break;
} }
list_add_rcu(&new->list, elem->list.prev); list_add_rcu(&entry->ops.list, elem->list.prev);
mutex_unlock(&nf_hook_mutex); mutex_unlock(&nf_hook_mutex);
#ifdef CONFIG_NETFILTER_INGRESS #ifdef CONFIG_NETFILTER_INGRESS
if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS) if (reg->pf == NFPROTO_NETDEV && reg->hooknum == NF_NETDEV_INGRESS)
...@@ -122,6 +123,7 @@ EXPORT_SYMBOL(nf_register_net_hook); ...@@ -122,6 +123,7 @@ EXPORT_SYMBOL(nf_register_net_hook);
void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg) void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
{ {
struct list_head *nf_hook_list; struct list_head *nf_hook_list;
struct nf_hook_entry *entry;
struct nf_hook_ops *elem; struct nf_hook_ops *elem;
nf_hook_list = find_nf_hook_list(net, reg); nf_hook_list = find_nf_hook_list(net, reg);
...@@ -130,14 +132,9 @@ void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg) ...@@ -130,14 +132,9 @@ void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
mutex_lock(&nf_hook_mutex); mutex_lock(&nf_hook_mutex);
list_for_each_entry(elem, nf_hook_list, list) { list_for_each_entry(elem, nf_hook_list, list) {
if ((reg->hook == elem->hook) && entry = container_of(elem, struct nf_hook_entry, ops);
(reg->dev == elem->dev) && if (entry->orig_ops == reg) {
(reg->owner == elem->owner) && list_del_rcu(&entry->ops.list);
(reg->priv == elem->priv) &&
(reg->pf == elem->pf) &&
(reg->hooknum == elem->hooknum) &&
(reg->priority == elem->priority)) {
list_del_rcu(&elem->list);
break; break;
} }
} }
...@@ -154,8 +151,8 @@ void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg) ...@@ -154,8 +151,8 @@ void nf_unregister_net_hook(struct net *net, const struct nf_hook_ops *reg)
static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]); static_key_slow_dec(&nf_hooks_needed[reg->pf][reg->hooknum]);
#endif #endif
synchronize_net(); synchronize_net();
nf_queue_nf_hook_drop(net, elem); nf_queue_nf_hook_drop(net, &entry->ops);
kfree(elem); kfree(entry);
} }
EXPORT_SYMBOL(nf_unregister_net_hook); EXPORT_SYMBOL(nf_unregister_net_hook);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment