Skip to content

Commit 64b8763

Browse files
Liping Zhangummakynes
authored andcommitted
netfilter: conntrack: fix race between nf_conntrack proc read and hash resize
When we do "cat /proc/net/nf_conntrack", and meanwhile resize the conntrack hash table via /sys/module/nf_conntrack/parameters/hashsize, race will happen, because reader can observe a newly allocated hash but the old size (or vice versa). So oops will happen like follows: BUG: unable to handle kernel NULL pointer dereference at 0000000000000017 IP: [<ffffffffa0418e21>] seq_print_acct+0x11/0x50 [nf_conntrack] Call Trace: [<ffffffffa0412f4e>] ? ct_seq_show+0x14e/0x340 [nf_conntrack] [<ffffffff81261a1c>] seq_read+0x2cc/0x390 [<ffffffff812a8d62>] proc_reg_read+0x42/0x70 [<ffffffff8123bee7>] __vfs_read+0x37/0x130 [<ffffffff81347980>] ? security_file_permission+0xa0/0xc0 [<ffffffff8123cf75>] vfs_read+0x95/0x140 [<ffffffff8123e475>] SyS_read+0x55/0xc0 [<ffffffff817c2572>] entry_SYSCALL_64_fastpath+0x1a/0xa4 It is very easy to reproduce this kernel crash. 1. open one shell and input the following cmds: while : ; do echo $RANDOM > /sys/module/nf_conntrack/parameters/hashsize done 2. open more shells and input the following cmds: while : ; do cat /proc/net/nf_conntrack done 3. just wait a monent, oops will happen soon. The solution in this patch is based on Florian's Commit 5e3c61f ("netfilter: conntrack: fix lookup race during hash resize"). And add a wrapper function nf_conntrack_get_ht to get hash and hsize suggested by Florian Westphal. Signed-off-by: Liping Zhang <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent a90a6e5 commit 64b8763

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

include/net/netfilter/nf_conntrack_core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ bool nf_ct_invert_tuple(struct nf_conntrack_tuple *inverse,
5151
const struct nf_conntrack_l3proto *l3proto,
5252
const struct nf_conntrack_l4proto *l4proto);
5353

54+
void nf_conntrack_get_ht(struct hlist_nulls_head **hash, unsigned int *hsize);
55+
5456
/* Find a connection corresponding to a tuple. */
5557
struct nf_conntrack_tuple_hash *
5658
nf_conntrack_find_get(struct net *net,

net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
struct ct_iter_state {
2828
struct seq_net_private p;
29+
struct hlist_nulls_head *hash;
30+
unsigned int htable_size;
2931
unsigned int bucket;
3032
};
3133

@@ -35,10 +37,10 @@ static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
3537
struct hlist_nulls_node *n;
3638

3739
for (st->bucket = 0;
38-
st->bucket < nf_conntrack_htable_size;
40+
st->bucket < st->htable_size;
3941
st->bucket++) {
4042
n = rcu_dereference(
41-
hlist_nulls_first_rcu(&nf_conntrack_hash[st->bucket]));
43+
hlist_nulls_first_rcu(&st->hash[st->bucket]));
4244
if (!is_a_nulls(n))
4345
return n;
4446
}
@@ -53,11 +55,11 @@ static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
5355
head = rcu_dereference(hlist_nulls_next_rcu(head));
5456
while (is_a_nulls(head)) {
5557
if (likely(get_nulls_value(head) == st->bucket)) {
56-
if (++st->bucket >= nf_conntrack_htable_size)
58+
if (++st->bucket >= st->htable_size)
5759
return NULL;
5860
}
5961
head = rcu_dereference(
60-
hlist_nulls_first_rcu(&nf_conntrack_hash[st->bucket]));
62+
hlist_nulls_first_rcu(&st->hash[st->bucket]));
6163
}
6264
return head;
6365
}
@@ -75,7 +77,11 @@ static struct hlist_nulls_node *ct_get_idx(struct seq_file *seq, loff_t pos)
7577
static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
7678
__acquires(RCU)
7779
{
80+
struct ct_iter_state *st = seq->private;
81+
7882
rcu_read_lock();
83+
84+
nf_conntrack_get_ht(&st->hash, &st->htable_size);
7985
return ct_get_idx(seq, *pos);
8086
}
8187

net/netfilter/nf_conntrack_core.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,23 @@ nf_ct_key_equal(struct nf_conntrack_tuple_hash *h,
460460
net_eq(net, nf_ct_net(ct));
461461
}
462462

463+
/* must be called with rcu read lock held */
464+
void nf_conntrack_get_ht(struct hlist_nulls_head **hash, unsigned int *hsize)
465+
{
466+
struct hlist_nulls_head *hptr;
467+
unsigned int sequence, hsz;
468+
469+
do {
470+
sequence = read_seqcount_begin(&nf_conntrack_generation);
471+
hsz = nf_conntrack_htable_size;
472+
hptr = nf_conntrack_hash;
473+
} while (read_seqcount_retry(&nf_conntrack_generation, sequence));
474+
475+
*hash = hptr;
476+
*hsize = hsz;
477+
}
478+
EXPORT_SYMBOL_GPL(nf_conntrack_get_ht);
479+
463480
/*
464481
* Warning :
465482
* - Caller must take a reference on returned object

net/netfilter/nf_conntrack_standalone.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ EXPORT_SYMBOL_GPL(print_tuple);
4848

4949
struct ct_iter_state {
5050
struct seq_net_private p;
51+
struct hlist_nulls_head *hash;
52+
unsigned int htable_size;
5153
unsigned int bucket;
5254
u_int64_t time_now;
5355
};
@@ -58,9 +60,10 @@ static struct hlist_nulls_node *ct_get_first(struct seq_file *seq)
5860
struct hlist_nulls_node *n;
5961

6062
for (st->bucket = 0;
61-
st->bucket < nf_conntrack_htable_size;
63+
st->bucket < st->htable_size;
6264
st->bucket++) {
63-
n = rcu_dereference(hlist_nulls_first_rcu(&nf_conntrack_hash[st->bucket]));
65+
n = rcu_dereference(
66+
hlist_nulls_first_rcu(&st->hash[st->bucket]));
6467
if (!is_a_nulls(n))
6568
return n;
6669
}
@@ -75,12 +78,11 @@ static struct hlist_nulls_node *ct_get_next(struct seq_file *seq,
7578
head = rcu_dereference(hlist_nulls_next_rcu(head));
7679
while (is_a_nulls(head)) {
7780
if (likely(get_nulls_value(head) == st->bucket)) {
78-
if (++st->bucket >= nf_conntrack_htable_size)
81+
if (++st->bucket >= st->htable_size)
7982
return NULL;
8083
}
8184
head = rcu_dereference(
82-
hlist_nulls_first_rcu(
83-
&nf_conntrack_hash[st->bucket]));
85+
hlist_nulls_first_rcu(&st->hash[st->bucket]));
8486
}
8587
return head;
8688
}
@@ -102,6 +104,8 @@ static void *ct_seq_start(struct seq_file *seq, loff_t *pos)
102104

103105
st->time_now = ktime_get_real_ns();
104106
rcu_read_lock();
107+
108+
nf_conntrack_get_ht(&st->hash, &st->htable_size);
105109
return ct_get_idx(seq, *pos);
106110
}
107111

0 commit comments

Comments
 (0)