Received: by 2002:a05:6520:4211:b029:f4:110d:56bc with SMTP id o17csp2118686lkv; Thu, 20 May 2021 03:51:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy1cC3PIMTN1sxTVKj5DfYCZIlSHH0CeYKj6Ch/mDBrJ4nWAqQX81TcEoBLzdeeAOtJUuY9 X-Received: by 2002:a17:906:7302:: with SMTP id di2mr3946706ejc.409.1621507883480; Thu, 20 May 2021 03:51:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621507883; cv=none; d=google.com; s=arc-20160816; b=pt8/8njRR+MtM0hTDI9sCmuvXVIcJN8a07a+55Qp8gnFjOTSsRHFgG5ZJliC0LWMQB ISuvKC6aY1QoXFP854RG3zrzrlxf0rISFK+F7R/QSO13QnFtnXMzSRrlhq85t5rmewfp ql/B1bursZFQ1Y0s0+Rm8qhrl93wZU2PRRFpwe5FAQx5A4XYQHLUSX7e/rFOZxr52EPc NPvnNEknZR7tHosL+3c68wee8DLOAp8w2qCt72ZKoLrjjvYu49noV4l6/yukCd2v3R/8 /K2+aN8Vdgv+bEsUZHKwD7EtimY3xSFS9ALRZSc7TBqcThFcS/DgDsRgaJuvTCJ8b3O9 X6AQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Cm2nnWN9LiYsOMpV44DQhB8Dq58GamVQxoTCHY4Ixzs=; b=hj2H42hD/r7OUXYmwfn0UMZoxjDKENHkuuHul2qzGKZA7dLw90qR83f6p9G065AJY8 fyskUHxyHObjkaGnR1PeUa7BA0Glvc00nKT6Cc0oJrntFe4e0t6xVi4KqwC1vQV/S3tk sCZZ+/TwTSVGzkN+umMkG56D/Xy95bS+lCRjk0DBX1vs2knMFZBVN4MyArtYD+CJX5zB HcsZ/1WJLqoWMn2Tg60Oj87zhzqdfkgxgjpPEdaOpOrOQLy5fWmAnbQinTTUqQppT3ju 9IfxNBjTFblvi27FkThz7EgKxBnscNnFMflmOikW33Wp+ZJ/JXguI9unpGSsDYPJJeaA slUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=bWGef1ha; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id am8si2194096ejc.753.2021.05.20.03.50.55; Thu, 20 May 2021 03:51:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=bWGef1ha; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238883AbhETKuZ (ORCPT + 99 others); Thu, 20 May 2021 06:50:25 -0400 Received: from mail.kernel.org ([198.145.29.99]:37944 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237604AbhETKfC (ORCPT ); Thu, 20 May 2021 06:35:02 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7873861C5A; Thu, 20 May 2021 09:53:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1621504404; bh=VbWvpjUzcW5D73HM8JgB+HEcKlV0It1nJPzYmb4qJ3U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bWGef1haJJfWbtbYgcId0kfXnp5fmk7e5Xq1HxDQTbqvORWG36p3l5gauWmifGoil yJ8ifmESU1nYug7PsG2mlS8nVZtZKIRSSLjP+xlAW/dUoxQFafHtK+wYZnCCfpGXfo GNS7hY522HUChL8deixSF584Z/FTKGSmy+61gH4M= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Or Cohen , Xin Long , "David S. Miller" Subject: [PATCH 4.14 230/323] sctp: delay auto_asconf init until binding the first addr Date: Thu, 20 May 2021 11:22:02 +0200 Message-Id: <20210520092128.047001561@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210520092120.115153432@linuxfoundation.org> References: <20210520092120.115153432@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Xin Long commit 34e5b01186858b36c4d7c87e1a025071e8e2401f upstream. As Or Cohen described: If sctp_destroy_sock is called without sock_net(sk)->sctp.addr_wq_lock held and sp->do_auto_asconf is true, then an element is removed from the auto_asconf_splist without any proper locking. This can happen in the following functions: 1. In sctp_accept, if sctp_sock_migrate fails. 2. In inet_create or inet6_create, if there is a bpf program attached to BPF_CGROUP_INET_SOCK_CREATE which denies creation of the sctp socket. This patch is to fix it by moving the auto_asconf init out of sctp_init_sock(), by which inet_create()/inet6_create() won't need to operate it in sctp_destroy_sock() when calling sk_common_release(). It also makes more sense to do auto_asconf init while binding the first addr, as auto_asconf actually requires an ANY addr bind, see it in sctp_addr_wq_timeout_handler(). This addresses CVE-2021-23133. Fixes: 610236587600 ("bpf: Add new cgroup attach type to enable sock modifications") Reported-by: Or Cohen Signed-off-by: Xin Long Signed-off-by: David S. Miller Signed-off-by: Greg Kroah-Hartman --- net/sctp/socket.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) --- a/net/sctp/socket.c +++ b/net/sctp/socket.c @@ -369,6 +369,18 @@ static struct sctp_af *sctp_sockaddr_af( return af; } +static void sctp_auto_asconf_init(struct sctp_sock *sp) +{ + struct net *net = sock_net(&sp->inet.sk); + + if (net->sctp.default_auto_asconf) { + spin_lock(&net->sctp.addr_wq_lock); + list_add_tail(&sp->auto_asconf_list, &net->sctp.auto_asconf_splist); + spin_unlock(&net->sctp.addr_wq_lock); + sp->do_auto_asconf = 1; + } +} + /* Bind a local address either to an endpoint or to an association. */ static int sctp_do_bind(struct sock *sk, union sctp_addr *addr, int len) { @@ -431,8 +443,10 @@ static int sctp_do_bind(struct sock *sk, } /* Refresh ephemeral port. */ - if (!bp->port) + if (!bp->port) { bp->port = inet_sk(sk)->inet_num; + sctp_auto_asconf_init(sp); + } /* Add the address to the bind address list. * Use GFP_ATOMIC since BHs will be disabled. @@ -4449,19 +4463,6 @@ static int sctp_init_sock(struct sock *s sk_sockets_allocated_inc(sk); sock_prot_inuse_add(net, sk->sk_prot, 1); - /* Nothing can fail after this block, otherwise - * sctp_destroy_sock() will be called without addr_wq_lock held - */ - if (net->sctp.default_auto_asconf) { - spin_lock(&sock_net(sk)->sctp.addr_wq_lock); - list_add_tail(&sp->auto_asconf_list, - &net->sctp.auto_asconf_splist); - sp->do_auto_asconf = 1; - spin_unlock(&sock_net(sk)->sctp.addr_wq_lock); - } else { - sp->do_auto_asconf = 0; - } - local_bh_enable(); return 0; @@ -8219,6 +8220,8 @@ static void sctp_sock_migrate(struct soc sctp_bind_addr_dup(&newsp->ep->base.bind_addr, &oldsp->ep->base.bind_addr, GFP_KERNEL); + sctp_auto_asconf_init(newsp); + /* Move any messages in the old socket's receive queue that are for the * peeled off association to the new socket's receive queue. */