From: Eric Dumazet Date: Tue, 14 Feb 2017 17:03:51 +0000 (-0800) Subject: packet: fix races in fanout_add() X-Git-Tag: v4.1.12-117.0_27200813_3~16 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=8331671104bba215384f42be7684f13eb7cadd36;p=users%2Fjedix%2Flinux-maple.git packet: fix races in fanout_add() 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: dc99f600698d ("packet: Add fanout support.") Fixes: 0648ab70afe6 ("packet: rollover prepare: per-socket state") Signed-off-by: Eric Dumazet Cc: Willem de Bruijn Signed-off-by: David S. Miller (cherry picked from commit d199fab63c11998a602205f7ee7ff7c05c97164b) Orabug: 27050772 CVE: CVE-2017-15649 Signed-off-by: Kirtikar Kashyap Reviewed-by: Jack Vogel Conflicts: net/packet/af_packet.c Left out the changes related to rollover --- diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index 368105cf2f7a..0cdea22910d3 100644 --- a/net/packet/af_packet.c +++ b/net/packet/af_packet.c @@ -1447,13 +1447,16 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags) return -EINVAL; } + mutex_lock(&fanout_mutex); + + err = -EINVAL; if (!po->running) - return -EINVAL; + goto out; + err = -EALREADY; if (po->fanout) - return -EALREADY; + goto out; - mutex_lock(&fanout_mutex); match = NULL; list_for_each_entry(f, &fanout_list, list) { if (f->id == id && @@ -1509,17 +1512,17 @@ static void fanout_release(struct sock *sk) struct packet_sock *po = pkt_sk(sk); struct packet_fanout *f; + mutex_lock(&fanout_mutex); f = po->fanout; - if (!f) - return; + if (f) { + po->fanout = NULL; - mutex_lock(&fanout_mutex); - po->fanout = NULL; + if (atomic_dec_and_test(&f->sk_ref)) { + list_del(&f->list); + dev_remove_pack(&f->prot_hook); + kfree(f); + } - if (atomic_dec_and_test(&f->sk_ref)) { - list_del(&f->list); - dev_remove_pack(&f->prot_hook); - kfree(f); } mutex_unlock(&fanout_mutex); }