Skip to content

Commit dc3ee32

Browse files
ebiedermummakynes
authored andcommitted
netfilter: nf_queue: Make the queue_handler pernet
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>
1 parent 720b287 commit dc3ee32

4 files changed

Lines changed: 24 additions & 17 deletions

File tree

‎include/net/netfilter/nf_queue.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ struct nf_queue_handler {
2828
struct nf_hook_ops *ops);
2929
};
3030

31-
void nf_register_queue_handler(const struct nf_queue_handler *qh);
32-
void nf_unregister_queue_handler(void);
31+
void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh);
32+
void nf_unregister_queue_handler(struct net *net);
3333
void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict);
3434

3535
void nf_queue_entry_get_refs(struct nf_queue_entry *entry);

‎include/net/netns/netfilter.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
struct proc_dir_entry;
77
struct nf_logger;
8+
struct nf_queue_handler;
89

910
struct netns_nf {
1011
#if defined CONFIG_PROC_FS
1112
struct proc_dir_entry *proc_netfilter;
1213
#endif
14+
const struct nf_queue_handler __rcu *queue_handler;
1315
const struct nf_logger __rcu *nf_loggers[NFPROTO_NUMPROTO];
1416
#ifdef CONFIG_SYSCTL
1517
struct ctl_table_header *nf_log_dir_header;

‎net/netfilter/nf_queue.c‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,21 @@
2626
* Once the queue is registered it must reinject all packets it
2727
* receives, no matter what.
2828
*/
29-
static const struct nf_queue_handler __rcu *queue_handler __read_mostly;
3029

3130
/* return EBUSY when somebody else is registered, return EEXIST if the
3231
* same handler is registered, return 0 in case of success. */
33-
void nf_register_queue_handler(const struct nf_queue_handler *qh)
32+
void nf_register_queue_handler(struct net *net, const struct nf_queue_handler *qh)
3433
{
3534
/* should never happen, we only have one queueing backend in kernel */
36-
WARN_ON(rcu_access_pointer(queue_handler));
37-
rcu_assign_pointer(queue_handler, qh);
35+
WARN_ON(rcu_access_pointer(net->nf.queue_handler));
36+
rcu_assign_pointer(net->nf.queue_handler, qh);
3837
}
3938
EXPORT_SYMBOL(nf_register_queue_handler);
4039

4140
/* The caller must flush their queue before this */
42-
void nf_unregister_queue_handler(void)
41+
void nf_unregister_queue_handler(struct net *net)
4342
{
44-
RCU_INIT_POINTER(queue_handler, NULL);
45-
synchronize_rcu();
43+
RCU_INIT_POINTER(net->nf.queue_handler, NULL);
4644
}
4745
EXPORT_SYMBOL(nf_unregister_queue_handler);
4846

@@ -103,7 +101,7 @@ void nf_queue_nf_hook_drop(struct net *net, struct nf_hook_ops *ops)
103101
const struct nf_queue_handler *qh;
104102

105103
rcu_read_lock();
106-
qh = rcu_dereference(queue_handler);
104+
qh = rcu_dereference(net->nf.queue_handler);
107105
if (qh)
108106
qh->nf_hook_drop(net, ops);
109107
rcu_read_unlock();
@@ -122,9 +120,10 @@ int nf_queue(struct sk_buff *skb,
122120
struct nf_queue_entry *entry = NULL;
123121
const struct nf_afinfo *afinfo;
124122
const struct nf_queue_handler *qh;
123+
struct net *net = state->net;
125124

126125
/* QUEUE == DROP if no one is waiting, to be safe. */
127-
qh = rcu_dereference(queue_handler);
126+
qh = rcu_dereference(net->nf.queue_handler);
128127
if (!qh) {
129128
status = -ESRCH;
130129
goto err;

‎net/netfilter/nfnetlink_queue.c‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,21 +1377,29 @@ static int __net_init nfnl_queue_net_init(struct net *net)
13771377
net->nf.proc_netfilter, &nfqnl_file_ops))
13781378
return -ENOMEM;
13791379
#endif
1380+
nf_register_queue_handler(net, &nfqh);
13801381
return 0;
13811382
}
13821383

13831384
static void __net_exit nfnl_queue_net_exit(struct net *net)
13841385
{
1386+
nf_unregister_queue_handler(net);
13851387
#ifdef CONFIG_PROC_FS
13861388
remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter);
13871389
#endif
13881390
}
13891391

1392+
static void nfnl_queue_net_exit_batch(struct list_head *net_exit_list)
1393+
{
1394+
synchronize_rcu();
1395+
}
1396+
13901397
static struct pernet_operations nfnl_queue_net_ops = {
1391-
.init = nfnl_queue_net_init,
1392-
.exit = nfnl_queue_net_exit,
1393-
.id = &nfnl_queue_net_id,
1394-
.size = sizeof(struct nfnl_queue_net),
1398+
.init = nfnl_queue_net_init,
1399+
.exit = nfnl_queue_net_exit,
1400+
.exit_batch = nfnl_queue_net_exit_batch,
1401+
.id = &nfnl_queue_net_id,
1402+
.size = sizeof(struct nfnl_queue_net),
13951403
};
13961404

13971405
static int __init nfnetlink_queue_init(void)
@@ -1412,7 +1420,6 @@ static int __init nfnetlink_queue_init(void)
14121420
}
14131421

14141422
register_netdevice_notifier(&nfqnl_dev_notifier);
1415-
nf_register_queue_handler(&nfqh);
14161423
return status;
14171424

14181425
cleanup_netlink_notifier:
@@ -1424,7 +1431,6 @@ static int __init nfnetlink_queue_init(void)
14241431

14251432
static void __exit nfnetlink_queue_fini(void)
14261433
{
1427-
nf_unregister_queue_handler();
14281434
unregister_netdevice_notifier(&nfqnl_dev_notifier);
14291435
nfnetlink_subsys_unregister(&nfqnl_subsys);
14301436
netlink_unregister_notifier(&nfqnl_rtnl_notifier);

0 commit comments

Comments
 (0)