Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4637984pxj; Wed, 12 May 2021 09:47:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy171aY5qOg+UQl2b/em9l1w1JNgnIKSvYVazZxrlzWDxLQLHi68Y/BNEmw9s46821n4Nli X-Received: by 2002:a05:651c:31b:: with SMTP id a27mr30151559ljp.324.1620838038016; Wed, 12 May 2021 09:47:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620838038; cv=none; d=google.com; s=arc-20160816; b=NUbXsx3tWnryTnUvIpbetxXbF5DmBHUTjRhOhjriKnSXOKux0BdbkosBMLipexqLcb YRUI7+E4UuCfGRtH1ftNpqumi+Bipj1LJdV89sHeo2reGn39n49bTMwtqv9xxNS6CJ9w WbdYbj9DHdbO3Dy5j7hWccpEit5uuZrX39SEqEvwkGLTyMuya0ZAI9QJow2rPjXZFelu qV7uX7Fkb/cRnVG4tfEmeK2Qt7SZ2UVKE2r8Y664Um0FQRQrA40bA3Kk9oEwTrh1m2bd 27kUwsk82ap/U3NQfKoI7lL2y9c76mI3qX8GJPcuV87UCXxkQCtLhD2tV7u9K9vMJ3cI qwRg== 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=zGBtgYm/k3lsJKZalpdp9O8lTP/Fzv9ZK8A0/Lkh6eg=; b=KHPByJt3str/mioVaWGq6tdzr1wVj1EDf+lS/TY4C/lsCpp4OGvDIPpEdwbMt20jfg yTHJxgiVH2F7NKPaVNN576l9BQm3Fyni9PgK1NbFHqq77Kcg4PT8J5YRC/oRMbDbA6uC tdE995NXf/7lmu0jcGb4D4j1qpk6kwseFT6rlYLH1yK/XgkMUCMj6787nAtU+KuE25A6 8z0UO/9w292uHs15OJ8BgkaIfF7RA8zCsRn/d1UMraRe25RxHR+5/ErMZQycx/16dtd1 t1kOEtFg5Vc2gWMdleJ26+SiqRN9MYi27vF93PiLweSHPnqw+NbrTn2xFobCC6mjG04K qHcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=08rUecc+; 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 o20si544802lfu.226.2021.05.12.09.46.48; Wed, 12 May 2021 09:47:18 -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=08rUecc+; 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 S239570AbhELQpK (ORCPT + 99 others); Wed, 12 May 2021 12:45:10 -0400 Received: from mail.kernel.org ([198.145.29.99]:50084 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236976AbhELPrl (ORCPT ); Wed, 12 May 2021 11:47:41 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 278A461CA8; Wed, 12 May 2021 15:24:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620833051; bh=KSK4APaPSeTB//NlRhdv8xPGF18qRoyCi8t649TVLRs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=08rUecc+AXvDv72h/awpXP+nF1h0bkut86S4Vwz120PsblCa/vsyjHlOa/XQJ7BEH 6iR5xkR5n54Ju28gC0x+nDWqr1wyyWje2amugpmcekSHCVNN0Lrq7sOvWvSA+G7/Ag xbuaCVi32Obgv0j8iW4f+k4kC9TPx9NggBcvw7s4= 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 5.10 530/530] sctp: delay auto_asconf init until binding the first addr Date: Wed, 12 May 2021 16:50:40 +0200 Message-Id: <20210512144837.158026354@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144819.664462530@linuxfoundation.org> References: <20210512144819.664462530@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 @@ -357,6 +357,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) { @@ -418,8 +430,10 @@ static int sctp_do_bind(struct sock *sk, return -EADDRINUSE; /* 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. @@ -4939,19 +4953,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; @@ -9285,6 +9286,8 @@ static int sctp_sock_migrate(struct sock return err; } + 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. */