From patchwork Wed Oct 19 08:26:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 5578 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp352608wrs; Wed, 19 Oct 2022 07:17:11 -0700 (PDT) X-Google-Smtp-Source: AMsMyM58eaLHXfm7wLaK/1s1XAeyyAHNnGcZ95uHnAJhlTuEtPHPBULLEuOytMbsX8OKb2uvXYvn X-Received: by 2002:a17:907:a047:b0:78d:b8ac:765d with SMTP id gz7-20020a170907a04700b0078db8ac765dmr6887618ejc.631.1666189031344; Wed, 19 Oct 2022 07:17:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666189031; cv=none; d=google.com; s=arc-20160816; b=aA3Y1oJ1b8LBhgC8wLLMp4aDThzJbUxosTzzl5O9T67dvxxVRzB5XJ2qJeUd8j53o5 7qBipCKM5ZK+Izi9ITPRblnS1tqq5rUzBVH0+z4NK8J4ht9j+Nn5HPScUz+CwNBEC+0E BY2H/gKUS5MdPZv/bDdhjxJP6jGiaGrUKCFyHyYu2eo2EPzLg36IExcGEgmPhkac80Kn 3jbCYJrVMbZ1sCcezGOlaryfw5aHW9/cYM/OV/vIokHNRYVa4TY+HRO5nHvKFYnSOwxw xekbEnMbdrwB3QmpOnGZYgEfNqXIAEpIi2WqHZckuoXplGS8TywPLW5qrEcjLDeCBb34 zxQg== 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=MJiHNpUwlWVmHZBlItDu2gOvNctMQDTjBQhb52yzNH2ir6xD3X6KAloEnU2pmINWtr eEHURZ5dnidJWvIlxB/3ZMCrz9CvfWTV/w8HQBrHiMD0/tEKq8GZ6kFqKTIOg7lUnveq vvrzSaS67Zs/i5mP+RjWGAlPvgxxWretq9CPxDAZDs5FpGDzjixeGHLP1Q3f3OCEhjoa 3wKfr3M7fXwy7mbS5SwhqkPvqp0z2pbPKWSg7aXB7BDaqoa7tN5uRKCaOU1CB10+F0+X BUt40nvP1ky7XIFYmj2UrSzWrsTfyHtbrUvnYiOuqwQF1cwnn7EZkdAsrz/i6fd2w9CI OK6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=o3YpWtus; 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 h16-20020a05640250d000b0045a1e2373dbsi16430235edb.44.2022.10.19.07.16.35; Wed, 19 Oct 2022 07:17:11 -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=o3YpWtus; 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 S230424AbiJSOGQ (ORCPT + 99 others); Wed, 19 Oct 2022 10:06:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231197AbiJSOFy (ORCPT ); Wed, 19 Oct 2022 10:05:54 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6ED7E18F0EA; Wed, 19 Oct 2022 06:47:24 -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 ams.source.kernel.org (Postfix) with ESMTPS id 53D05B823A6; Wed, 19 Oct 2022 08:53:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ACDF9C433C1; Wed, 19 Oct 2022 08:53:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666169585; bh=jOc7LW7IWzijjP0DhfLG0KzytqA6AuX/PkOEIoEOpsI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o3YpWtusGJWny0DDTbl5HgnPAAHms+odBVXZnIn01lPv15rB+fSvr8om7gqMMvI87 unj1Haqk01aKYjtCKAi/jh3Kj54Zh8tU3ObitecEjcLOyyAVl2NQOa7bg/psBCQFuD /XHsKuk4s38qWmoey+6bvbnUmYCGN1vgMD8UL6Uw= 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 6.0 331/862] sctp: handle the error returned from sctp_auth_asoc_init_active_key Date: Wed, 19 Oct 2022 10:26:58 +0200 Message-Id: <20221019083304.672045967@linuxfoundation.org> X-Mailer: git-send-email 2.38.0 In-Reply-To: <20221019083249.951566199@linuxfoundation.org> References: <20221019083249.951566199@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.4 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?1747125829530842064?= X-GMAIL-MSGID: =?utf-8?q?1747125829530842064?= 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;