![Eric W. Biederman](/assets/img/avatar_default.png)
Florian Weber reported: > Under full load (unshare() in loop -> OOM conditions) we can > get kernel panic: > > BUG: unable to handle kernel NULL pointer dereference at 0000000000000008 > IP: [<ffffffff81476c85>] nfqnl_nf_hook_drop+0x35/0x70 > [..] > task: ffff88012dfa3840 ti: ffff88012dffc000 task.ti: ffff88012dffc000 > RIP: 0010:[<ffffffff81476c85>] [<ffffffff81476c85>] nfqnl_nf_hook_drop+0x35/0x70 > RSP: 0000:ffff88012dfffd80 EFLAGS: 00010206 > RAX: 0000000000000008 RBX: ffffffff81add0c0 RCX: ffff88013fd80000 > [..] > Call Trace: > [<ffffffff81474d98>] nf_queue_nf_hook_drop+0x18/0x20 > [<ffffffff814738eb>] nf_unregister_net_hook+0xdb/0x150 > [<ffffffff8147398f>] netfilter_net_exit+0x2f/0x60 > [<ffffffff8141b088>] ops_exit_list.isra.4+0x38/0x60 > [<ffffffff8141b652>] setup_net+0xc2/0x120 > [<ffffffff8141bd09>] copy_net_ns+0x79/0x120 > [<ffffffff8106965b>] create_new_namespaces+0x11b/0x1e0 > [<ffffffff810698a7>] unshare_nsproxy_namespaces+0x57/0xa0 > [<ffffffff8104baa2>] SyS_unshare+0x1b2/0x340 > [<ffffffff81608276>] entry_SYSCALL_64_fastpath+0x1e/0xa8 > Code: 65 00 48 89 e5 41 56 41 55 41 54 53 83 e8 01 48 8b 97 70 12 00 00 48 98 49 89 f4 4c 8b 74 c2 18 4d 8d 6e 08 49 81 c6 88 00 00 00 <49> 8b 5d 00 48 85 db 74 1a 48 89 df 4c 89 e2 48 c7 c6 90 68 47 > The simple fix for this requires a new pernet variable for struct nf_queue that indicates when it is safe to use the dynamically allocated nf_queue state. As we need a variable anyway make nf_register_queue_handler and nf_unregister_queue_handler pernet. This allows the existing logic of when it is safe to use the state from the nfnetlink_queue module to be reused with no changes except for making it per net. The syncrhonize_rcu from nf_unregister_queue_handler is moved to a new function nfnl_queue_net_exit_batch so that the worst case of having a syncrhonize_rcu in the pernet exit path is not experienced in batch mode. Reported-by: Florian Westphal <fw@strlen.de> Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com> Acked-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
219 lines
5 KiB
C
219 lines
5 KiB
C
/*
|
|
* Rusty Russell (C)2000 -- This code is GPL.
|
|
* Patrick McHardy (c) 2006-2012
|
|
*/
|
|
|
|
#include <linux/kernel.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/init.h>
|
|
#include <linux/module.h>
|
|
#include <linux/proc_fs.h>
|
|
#include <linux/skbuff.h>
|
|
#include <linux/netfilter.h>
|
|
#include <linux/netfilter_bridge.h>
|
|
#include <linux/seq_file.h>
|
|
#include <linux/rcupdate.h>
|
|
#include <net/protocol.h>
|
|
#include <net/netfilter/nf_queue.h>
|
|
#include <net/dst.h>
|
|
|
|
#include "nf_internals.h"
|
|
|
|
/*
|
|
* Hook for nfnetlink_queue to register its queue handler.
|
|
* We do this so that most of the NFQUEUE code can be modular.
|
|
*
|
|
* Once the queue is registered it must reinject all packets it
|
|
* receives, no matter what.
|
|
*/
|
|
|
|
/* return EBUSY when somebody else is registered, return EEXIST if the
|
|
* same handler is registered, return 0 in case of success. */
|
|
void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh)
|
|
{
|
|
/* should never happen, we only have one queueing backend in kernel */
|
|
WARN_ON(rcu_access_pointer(net->nf.queue_handler));
|
|
rcu_assign_pointer(net->nf.queue_handler, qh);
|
|
}
|
|
EXPORT_SYMBOL(nf_register_queue_handler);
|
|
|
|
/* The caller must flush their queue before this */
|
|
void nf_unregister_queue_handler(struct net *net)
|
|
{
|
|
RCU_INIT_POINTER(net->nf.queue_handler, NULL);
|
|
}
|
|
EXPORT_SYMBOL(nf_unregister_queue_handler);
|
|
|
|
void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
|
|
{
|
|
struct nf_hook_state *state = &entry->state;
|
|
|
|
/* Release those devices we held, or Alexey will kill me. */
|
|
if (state->in)
|
|
dev_put(state->in);
|
|
if (state->out)
|
|
dev_put(state->out);
|
|
if (state->sk)
|
|
sock_put(state->sk);
|
|
#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
|
|
if (entry->skb->nf_bridge) {
|
|
struct net_device *physdev;
|
|
|
|
physdev = nf_bridge_get_physindev(entry->skb);
|
|
if (physdev)
|
|
dev_put(physdev);
|
|
physdev = nf_bridge_get_physoutdev(entry->skb);
|
|
if (physdev)
|
|
dev_put(physdev);
|
|
}
|
|
#endif
|
|
}
|
|
EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
|
|
|
|
/* Bump dev refs so they don't vanish while packet is out */
|
|
void nf_queue_entry_get_refs(struct nf_queue_entry *entry)
|
|
{
|
|
struct nf_hook_state *state = &entry->state;
|
|
|
|
if (state->in)
|
|
dev_hold(state->in);
|
|
if (state->out)
|
|
dev_hold(state->out);
|
|
if (state->sk)
|
|
sock_hold(state->sk);
|
|
#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
|
|
if (entry->skb->nf_bridge) {
|
|
struct net_device *physdev;
|
|
|
|
physdev = nf_bridge_get_physindev(entry->skb);
|
|
if (physdev)
|
|
dev_hold(physdev);
|
|
physdev = nf_bridge_get_physoutdev(entry->skb);
|
|
if (physdev)
|
|
dev_hold(physdev);
|
|
}
|
|
#endif
|
|
}
|
|
EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
|
|
|
|
void nf_queue_nf_hook_drop(struct net *net, struct nf_hook_ops *ops)
|
|
{
|
|
const struct nf_queue_handler *qh;
|
|
|
|
rcu_read_lock();
|
|
qh = rcu_dereference(net->nf.queue_handler);
|
|
if (qh)
|
|
qh->nf_hook_drop(net, ops);
|
|
rcu_read_unlock();
|
|
}
|
|
|
|
/*
|
|
* Any packet that leaves via this function must come back
|
|
* through nf_reinject().
|
|
*/
|
|
int nf_queue(struct sk_buff *skb,
|
|
struct nf_hook_ops *elem,
|
|
struct nf_hook_state *state,
|
|
unsigned int queuenum)
|
|
{
|
|
int status = -ENOENT;
|
|
struct nf_queue_entry *entry = NULL;
|
|
const struct nf_afinfo *afinfo;
|
|
const struct nf_queue_handler *qh;
|
|
struct net *net = state->net;
|
|
|
|
/* QUEUE == DROP if no one is waiting, to be safe. */
|
|
qh = rcu_dereference(net->nf.queue_handler);
|
|
if (!qh) {
|
|
status = -ESRCH;
|
|
goto err;
|
|
}
|
|
|
|
afinfo = nf_get_afinfo(state->pf);
|
|
if (!afinfo)
|
|
goto err;
|
|
|
|
entry = kmalloc(sizeof(*entry) + afinfo->route_key_size, GFP_ATOMIC);
|
|
if (!entry) {
|
|
status = -ENOMEM;
|
|
goto err;
|
|
}
|
|
|
|
*entry = (struct nf_queue_entry) {
|
|
.skb = skb,
|
|
.elem = elem,
|
|
.state = *state,
|
|
.size = sizeof(*entry) + afinfo->route_key_size,
|
|
};
|
|
|
|
nf_queue_entry_get_refs(entry);
|
|
skb_dst_force(skb);
|
|
afinfo->saveroute(skb, entry);
|
|
status = qh->outfn(entry, queuenum);
|
|
|
|
if (status < 0) {
|
|
nf_queue_entry_release_refs(entry);
|
|
goto err;
|
|
}
|
|
|
|
return 0;
|
|
|
|
err:
|
|
kfree(entry);
|
|
return status;
|
|
}
|
|
|
|
void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
|
|
{
|
|
struct sk_buff *skb = entry->skb;
|
|
struct nf_hook_ops *elem = entry->elem;
|
|
const struct nf_afinfo *afinfo;
|
|
int err;
|
|
|
|
nf_queue_entry_release_refs(entry);
|
|
|
|
/* Continue traversal iff userspace said ok... */
|
|
if (verdict == NF_REPEAT)
|
|
verdict = elem->hook(elem->priv, skb, &entry->state);
|
|
|
|
if (verdict == NF_ACCEPT) {
|
|
afinfo = nf_get_afinfo(entry->state.pf);
|
|
if (!afinfo || afinfo->reroute(entry->state.net, skb, entry) < 0)
|
|
verdict = NF_DROP;
|
|
}
|
|
|
|
entry->state.thresh = INT_MIN;
|
|
|
|
if (verdict == NF_ACCEPT) {
|
|
next_hook:
|
|
verdict = nf_iterate(entry->state.hook_list,
|
|
skb, &entry->state, &elem);
|
|
}
|
|
|
|
switch (verdict & NF_VERDICT_MASK) {
|
|
case NF_ACCEPT:
|
|
case NF_STOP:
|
|
local_bh_disable();
|
|
entry->state.okfn(entry->state.net, entry->state.sk, skb);
|
|
local_bh_enable();
|
|
break;
|
|
case NF_QUEUE:
|
|
err = nf_queue(skb, elem, &entry->state,
|
|
verdict >> NF_VERDICT_QBITS);
|
|
if (err < 0) {
|
|
if (err == -ESRCH &&
|
|
(verdict & NF_VERDICT_FLAG_QUEUE_BYPASS))
|
|
goto next_hook;
|
|
kfree_skb(skb);
|
|
}
|
|
break;
|
|
case NF_STOLEN:
|
|
break;
|
|
default:
|
|
kfree_skb(skb);
|
|
}
|
|
|
|
kfree(entry);
|
|
}
|
|
EXPORT_SYMBOL(nf_reinject);
|