Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp3378160ybp; Sun, 6 Oct 2019 10:36:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqznAtltq2Oe8HfNgdb5DWGO+u1D7esP5Fs2YRFBZKmc6aRxLKUC0yhOhr29Sv6xkHtZk9tU X-Received: by 2002:a17:906:5644:: with SMTP id v4mr20404511ejr.52.1570383411444; Sun, 06 Oct 2019 10:36:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1570383411; cv=none; d=google.com; s=arc-20160816; b=NTL4ixhske27DAoxDCo35qaHw1CSyzJ0yhpyXQtiYchSu9gL39oAAhK6f1NpnRTqQz BI7xR4ol0fJgOOAu06z3hv4IkOKXiIlW3f2oE+QBP+o9+JlCGdqs7g8oD80V/C9r1ET4 vTLtIKJGaGeP3tKZz0HJ7UjaDayOy0+UHWOKhcQWWevpgwgKdGzPbB3fQZlk29RJ7lo5 2lzoX0zuh/NsQKyfeBnvHaFWo/JZR3FqeuVXQjb6mQPbidm1NFIzlb17puc8WQHsX1s/ ZmYoqBDUpZYLBW29YYTshPavLzBPgrSlcIHlrXEjyHi591qAWIFvBD+27k722MqDK669 ub0Q== 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=2T+JBMMB0LIagyCi7kGX906fiOUk6bZIAVcwo/LHOY0=; b=kL4fKIx717h0EhsQilUd4WwaRWEvJ7yrx1IAL6cXCWv6nIwUzUCdpc0WfQ4fUH8+NG oh9ZoiYP7z7g5nXiIbjmHMen27YOCS7/aPBgfxY5QBV9TBuiDY1k0Jwrcg5/JqYgvIzC GHrsNnCsf4wIzRTKikCTT0ul+Y2USCir+4Gtc+oWmA/7YhPrFGkicLlUW+3gUsu2NnI4 Lly3OfO/9HLpFRfYocqrFMyK7FUkCWHwNeTFS6kvop93GJXw9QqxPufUQH+KbVSS7bOf 5nvhwZNZ7dyVPDsPakGJH+QLfzlKKUz3ermtXUfFz97fQDwZx3MEKQybnbRkLsA4NQmV uBjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IQ1G4bbI; 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 w2si7372490edc.386.2019.10.06.10.36.27; Sun, 06 Oct 2019 10:36:51 -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=IQ1G4bbI; 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 S1727458AbfJFRcp (ORCPT + 99 others); Sun, 6 Oct 2019 13:32:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:59208 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728484AbfJFRcn (ORCPT ); Sun, 6 Oct 2019 13:32:43 -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 E9D6221479; Sun, 6 Oct 2019 17:32:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1570383162; bh=AQXNI7teH9zY0Nt9DoZxtSd3C3DNrzzuqdtfyzz2xB0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IQ1G4bbIy0w9pybsGVjT4oh5YHFH+WbaiLaSnmgWAsfH1OrKgcJFxiS0b+sbthtVX G+QaXDErUhVSyonlEwdvaXP3XdEuL4UshmkqZAfLA6+w7jA/2CrDcp12kTcPY3N1/x 3yJjGm5+o93tM6mXCLOUoMTty0SdNiyiVd0x0LOw= 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 4.19 087/106] net: Unpublish sk from sk_reuseport_cb before call_rcu Date: Sun, 6 Oct 2019 19:21:33 +0200 Message-Id: <20191006171159.147815837@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191006171124.641144086@linuxfoundation.org> References: <20191006171124.641144086@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 @@ -1563,8 +1563,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); @@ -1587,7 +1585,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);