Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp3392057ybp; Sun, 6 Oct 2019 10:55:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqwULJNyVXdYzj4d3yc6WpmcEsxXIdADCj4B7W0qokxk1x6VVkgBqwwyoQ79wXF1NIy1cynJ X-Received: by 2002:a17:906:53d6:: with SMTP id p22mr20538391ejo.91.1570384553551; Sun, 06 Oct 2019 10:55:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570384553; cv=none; d=google.com; s=arc-20160816; b=IBnHOm9NVc6h6b/nUa+M5BfH567UFnQzYMFYXAjwHhMqYRkIFKhWX04UrAWfMUAc+p 9XwnCtrODdkNimIkMTlQ/gEOeopQjbXfMTFhD0SUJWOW3VXxbMftNpQmE7pMHFwjWcd+ hVYojGpI1O46cWmTRS59YvfNM0lWTKKbuyFCXjgTnA23JpMMMm31YKrcBrszM3nTgoB5 UAC0uHygUDmRBlSlzf83lnhDGAsUkR+Vss9RxDLGx7Fbi56vgDXnUvtMbkdI2Tf9USAI UVqzwZr9LpESd6jDoBnUQo2hP4vD1AasHlE3XmKHcszFt6DsYGISSl1PPhFou41GRJnb 5TXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=9ZD252YyUBu8q/j3Sw9d2xX9dJzzryE/xGu6SigEReo=; b=abVntG4YxNHgcOwfXsVgHT9ECDxY+QXSG6AhPN2jq1H9Bk45PNl8n8ZW1jax6aTcPP RkLPoNjdNx466+DJVtcg2MBwuEdzeEKtQ9enXZEerCpoU3ZUdSP3srR+z3Is47cKYyfk lMN95WrWS8WxZ7nFLdXlGMGpy10OlssyZGxN307Dylab1bhF+wR9pwAFhP9BGFGpaazZ WM1+8/KKl1h9C/RoDM0kGCfqDGpuKQ9TgcUY6UR4d563sbs8yRSt1rptlLPgn88JF+uO RHuHJ/ZRWedF4xNiw8YLCI7AYmbSoVH9z39o+EVegNWVDpsadL0NYTILsRWSA38/Oriz CIZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=TbCGLDAc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b50si7676134ede.28.2019.10.06.10.55.30; Sun, 06 Oct 2019 10:55:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=TbCGLDAc; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727673AbfJFRzH (ORCPT + 99 others); Sun, 6 Oct 2019 13:55:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:51344 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727316AbfJFRy2 (ORCPT ); Sun, 6 Oct 2019 13:54:28 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id D33C32247A; Sun, 6 Oct 2019 17:46:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570383995; bh=35T8UIzRCNiDttKKFdbMJmiuJrtXjpVVa+1qrFBi4Vo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TbCGLDAciGvHzQgp81pYY1BN/lcFQ0DyTvYJ9mNJcN41t2tVomlTMYWgQ7JRI0L3L gJP1hdqwpdkQHzYvYJi8eurWFYNqPs0fOahuDukGs4t5jo5xHzREETMA3VPI/uAZGm O2+ni9NrRoa/qdI0v7HBGMKS9x783kxAwrvymct0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Dumazet , Martin KaFai Lau , "David S. Miller" Subject: [PATCH 5.3 136/166] net: Unpublish sk from sk_reuseport_cb before call_rcu Date: Sun, 6 Oct 2019 19:21:42 +0200 Message-Id: <20191006171224.590814877@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191006171212.850660298@linuxfoundation.org> References: <20191006171212.850660298@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Martin KaFai Lau [ Upstream commit 8c7138b33e5c690c308b2a7085f6313fdcb3f616 ] The "reuse->sock[]" array is shared by multiple sockets. The going away sk must unpublish itself from "reuse->sock[]" before making call_rcu() call. However, this unpublish-action is currently done after a grace period and it may cause use-after-free. The fix is to move reuseport_detach_sock() to sk_destruct(). Due to the above reason, any socket with sk_reuseport_cb has to go through the rcu grace period before freeing it. It is a rather old bug (~3 yrs). The Fixes tag is not necessary the right commit but it is the one that introduced the SOCK_RCU_FREE logic and this fix is depending on it. Fixes: a4298e4522d6 ("net: add SOCK_RCU_FREE socket flag") Cc: Eric Dumazet Suggested-by: Eric Dumazet Signed-off-by: Martin KaFai Lau Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/core/sock.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1700,8 +1700,6 @@ static void __sk_destruct(struct rcu_hea sk_filter_uncharge(sk, filter); RCU_INIT_POINTER(sk->sk_filter, NULL); } - if (rcu_access_pointer(sk->sk_reuseport_cb)) - reuseport_detach_sock(sk); sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP); @@ -1728,7 +1726,14 @@ static void __sk_destruct(struct rcu_hea void sk_destruct(struct sock *sk) { - if (sock_flag(sk, SOCK_RCU_FREE)) + bool use_call_rcu = sock_flag(sk, SOCK_RCU_FREE); + + if (rcu_access_pointer(sk->sk_reuseport_cb)) { + reuseport_detach_sock(sk); + use_call_rcu = true; + } + + if (use_call_rcu) call_rcu(&sk->sk_rcu, __sk_destruct); else __sk_destruct(&sk->sk_rcu);