Commit bf791623 authored by Eric Dumazet's avatar Eric Dumazet Committed by Ben Hutchings

packet: fix races in fanout_add()

commit d199fab6 upstream.

Multiple threads can call fanout_add() at the same time.

We need to grab fanout_mutex earlier to avoid races that could
lead to one thread freeing po->rollover that was set by another thread.

Do the same in fanout_release(), for peace of mind, and to help us
finding lockdep issues earlier.

Fixes: dc99f600 ("packet: Add fanout support.")
Fixes: 0648ab70 ("packet: rollover prepare: per-socket state")
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Cc: Willem de Bruijn <willemb@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
[bwh: Backported to 3.16:
 - No rollover queue stats
 - Adjust context]
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
parent b08a436e
...@@ -1451,13 +1451,16 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags) ...@@ -1451,13 +1451,16 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
return -EINVAL; return -EINVAL;
} }
mutex_lock(&fanout_mutex);
err = -EINVAL;
if (!po->running) if (!po->running)
return -EINVAL; goto out;
err = -EALREADY;
if (po->fanout) if (po->fanout)
return -EALREADY; goto out;
mutex_lock(&fanout_mutex);
match = NULL; match = NULL;
list_for_each_entry(f, &fanout_list, list) { list_for_each_entry(f, &fanout_list, list) {
if (f->id == id && if (f->id == id &&
...@@ -1513,11 +1516,9 @@ static void fanout_release(struct sock *sk) ...@@ -1513,11 +1516,9 @@ static void fanout_release(struct sock *sk)
struct packet_sock *po = pkt_sk(sk); struct packet_sock *po = pkt_sk(sk);
struct packet_fanout *f; struct packet_fanout *f;
f = po->fanout;
if (!f)
return;
mutex_lock(&fanout_mutex); mutex_lock(&fanout_mutex);
f = po->fanout;
if (f) {
po->fanout = NULL; po->fanout = NULL;
if (atomic_dec_and_test(&f->sk_ref)) { if (atomic_dec_and_test(&f->sk_ref)) {
...@@ -1525,6 +1526,7 @@ static void fanout_release(struct sock *sk) ...@@ -1525,6 +1526,7 @@ static void fanout_release(struct sock *sk)
dev_remove_pack(&f->prot_hook); dev_remove_pack(&f->prot_hook);
kfree(f); kfree(f);
} }
}
mutex_unlock(&fanout_mutex); mutex_unlock(&fanout_mutex);
} }
......
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