From patchwork Mon Oct 24 11:28:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 9066 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp444596wru; Mon, 24 Oct 2022 06:11:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM70La/w/kOwhtMi9voEgpFZFiKBvfGsviYfKyXqygde2Z9k3UDKLXXANTPrVuYS+34Ja7KB X-Received: by 2002:a17:90a:540e:b0:210:1e26:9422 with SMTP id z14-20020a17090a540e00b002101e269422mr29540496pjh.100.1666617085483; Mon, 24 Oct 2022 06:11:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666617085; cv=none; d=google.com; s=arc-20160816; b=sPjPcQ65MpZJ/w8fqnCLsMIh5eQ+fFdEdOx6bxv6ohXKch1K8euVX/iWnOniHaz2BO 5p/KeqIEUR2sgnpCn0Yym0sohwqPbtRq+UrBAESIBXCkigxWR0YiF0+CDJpZBcNXnQWh wAkKALAnEW3/KcT3EADzE7g8ctDRVBhKFMGmssCUtRGu4Yq4JvlqDRRCE1Z635G6fnEE EBf2JoL1V6RJXvdfUgjxz38r1Meu45DvePOYPjBrBpXvzrA5dDd1BznEPSizX5riCoAA vtXPGWwKY/mzmRJ03ARi+Qg+TODo7nVKCCS89sd5u2OAOG7rc/2QCF4cfvTHFrdKtxCh /eVg== 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=g9z8YdGt90JpK2hCM+zsBv3a81ZKd8cUT021uMxF+YI=; b=w8Vd6DRUhUe3u3WrHDTa0cu3smXCGkrn6LmMiFKSq74s1Br3WGbD0m5Ep73T5JjISn jT1dtGOcYdtXu80bYWTC5plpyvFYzCv0iIvYvYOgbM4Q/c+cxw/7e8i7fLxdgXfWz0sG eYEMjkh6Uhn60VIOo1e8FoYNSuVd89FndH/qfQiM5Sce7QtfKQZgpp68umrcLP4hdBbL 2TxBl+8kGAdJd+21VCSIwib+JVJ0LaSOAFe9/g8mzFlA9tsTlqRvLTJhC34lOp4YKYnl CzVwR7SettHjA/w85dPXORDa7Bttk19L6CG5b2HJ1DQz/xs8Tr6lehIgP2ONONasp7Mf 0zjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=qbIqi1b7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l69-20020a638848000000b004622c80f523si32375703pgd.679.2022.10.24.06.11.08; Mon, 24 Oct 2022 06:11:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=qbIqi1b7; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S235504AbiJXNKx (ORCPT + 99 others); Mon, 24 Oct 2022 09:10:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235406AbiJXNIi (ORCPT ); Mon, 24 Oct 2022 09:08:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FD1E9E683; Mon, 24 Oct 2022 05:22:00 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id F2530612B3; Mon, 24 Oct 2022 12:21:43 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0EDC6C433D6; Mon, 24 Oct 2022 12:21:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666614103; bh=jOc7LW7IWzijjP0DhfLG0KzytqA6AuX/PkOEIoEOpsI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qbIqi1b7a62pfNEb3USTFnlA0YIk3s4IM8WVIkZiJ3Bam3UEOfadLoikrWEn9JIo5 wslyItDgcVkEf7/x+tr+Nzd2pPHpDnUncxMfa2rQRtk90WouYnLngdlbW6/IGB0QLK NHZSl7Lim30Pg7IICjbPYX58Z9dFFbZsEaO/7n4w= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+a236dd8e9622ed8954a3@syzkaller.appspotmail.com, Xin Long , "David S. Miller" , Sasha Levin Subject: [PATCH 5.10 133/390] sctp: handle the error returned from sctp_auth_asoc_init_active_key Date: Mon, 24 Oct 2022 13:28:50 +0200 Message-Id: <20221024113028.343180837@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113022.510008560@linuxfoundation.org> References: <20221024113022.510008560@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747574677213545766?= X-GMAIL-MSGID: =?utf-8?q?1747574677213545766?= From: Xin Long [ Upstream commit 022152aaebe116a25c39818a07e175a8cd3c1e11 ] When it returns an error from sctp_auth_asoc_init_active_key(), the active_key is actually not updated. The old sh_key will be freeed while it's still used as active key in asoc. Then an use-after-free will be triggered when sending patckets, as found by syzbot: sctp_auth_shkey_hold+0x22/0xa0 net/sctp/auth.c:112 sctp_set_owner_w net/sctp/socket.c:132 [inline] sctp_sendmsg_to_asoc+0xbd5/0x1a20 net/sctp/socket.c:1863 sctp_sendmsg+0x1053/0x1d50 net/sctp/socket.c:2025 inet_sendmsg+0x99/0xe0 net/ipv4/af_inet.c:819 sock_sendmsg_nosec net/socket.c:714 [inline] sock_sendmsg+0xcf/0x120 net/socket.c:734 This patch is to fix it by not replacing the sh_key when it returns errors from sctp_auth_asoc_init_active_key() in sctp_auth_set_key(). For sctp_auth_set_active_key(), old active_key_id will be set back to asoc->active_key_id when the same thing happens. Fixes: 58acd1009226 ("sctp: update active_key for asoc when old key is being replaced") Reported-by: syzbot+a236dd8e9622ed8954a3@syzkaller.appspotmail.com Signed-off-by: Xin Long Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/sctp/auth.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/net/sctp/auth.c b/net/sctp/auth.c index db6b7373d16c..34964145514e 100644 --- a/net/sctp/auth.c +++ b/net/sctp/auth.c @@ -863,12 +863,17 @@ int sctp_auth_set_key(struct sctp_endpoint *ep, } list_del_init(&shkey->key_list); - sctp_auth_shkey_release(shkey); list_add(&cur_key->key_list, sh_keys); - if (asoc && asoc->active_key_id == auth_key->sca_keynumber) - sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL); + if (asoc && asoc->active_key_id == auth_key->sca_keynumber && + sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL)) { + list_del_init(&cur_key->key_list); + sctp_auth_shkey_release(cur_key); + list_add(&shkey->key_list, sh_keys); + return -ENOMEM; + } + sctp_auth_shkey_release(shkey); return 0; } @@ -902,8 +907,13 @@ int sctp_auth_set_active_key(struct sctp_endpoint *ep, return -EINVAL; if (asoc) { + __u16 active_key_id = asoc->active_key_id; + asoc->active_key_id = key_id; - sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL); + if (sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL)) { + asoc->active_key_id = active_key_id; + return -ENOMEM; + } } else ep->active_key_id = key_id;