Return-Path: Received: from mail-bn3nam01on0135.outbound.protection.outlook.com ([104.47.33.135]:19456 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932186AbeARU72 (ORCPT ); Thu, 18 Jan 2018 15:59:28 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Eric Dumazet , "David S . Miller" , Sasha Levin Subject: [added to the 4.1 stable tree] packet: fix races in fanout_add() Date: Thu, 18 Jan 2018 20:59:25 +0000 Message-ID: <20180118205908.3220-5-alexander.levin@microsoft.com> References: <20180118205908.3220-1-alexander.levin@microsoft.com> In-Reply-To: <20180118205908.3220-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Eric Dumazet This patch has been added to the stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit d199fab63c11998a602205f7ee7ff7c05c97164b ] 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 Signed-off-by: Sasha Levin --- net/packet/af_packet.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c index 4cd7e27e3fc4..88cec9576e93 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; } =20 + mutex_lock(&fanout_mutex); + + err =3D -EINVAL; if (!po->running) - return -EINVAL; + goto out; =20 + err =3D -EALREADY; if (po->fanout) - return -EALREADY; + goto out; =20 - mutex_lock(&fanout_mutex); match =3D NULL; list_for_each_entry(f, &fanout_list, list) { if (f->id =3D=3D id && @@ -1509,17 +1512,16 @@ static void fanout_release(struct sock *sk) struct packet_sock *po =3D pkt_sk(sk); struct packet_fanout *f; =20 - f =3D po->fanout; - if (!f) - return; - mutex_lock(&fanout_mutex); - po->fanout =3D NULL; + f =3D po->fanout; + if (f) { + po->fanout =3D NULL; =20 - 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); } --=20 2.11.0