From patchwork Mon Oct 24 11:29:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 8905 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp434022wru; Mon, 24 Oct 2022 05:51:13 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5wGHjXAjrwisb/7SXUhgKYed5MkyYoki3+nawKrx2LMMySkan8GnoTSqwpAag5N47CpEwr X-Received: by 2002:a17:907:2c71:b0:79e:8603:72c6 with SMTP id ib17-20020a1709072c7100b0079e860372c6mr12943428ejc.172.1666615873173; Mon, 24 Oct 2022 05:51:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666615873; cv=none; d=google.com; s=arc-20160816; b=mQrvCr9ZoHeywEfi4Q4JIzwTjfcnEboIJeQQSTz/Jk1pBCez9UQzIOWE/wdlhGwujV OYs7xgPgLZFoGzoLxQBdYFCN9lnKD5rdrnyji8qqSEGutZsU5iqsQJlP95jov8ubRPzy tRMi6Ny84OC6xflh71rVUSeRPNtuIn5FLaNM3Zijql1bM/jEV7fQswMrHgj7tPFLV3XM kkj3Y2GfY3afb5ZV8dmg1paZamW/SZ0of5dBIgAZ6AuDeIWVs0SldI5ore+9Xf3QWxuO eXW/EVjCiyIVuWZ7MwjxhfGfbODThyp1MaKlUM6AFmOfqMUTtWHDx0V7jAtAS2T8HwKM WTDw== 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=JtEiJO89vNT2ZrdGD/0PXPOP5avgHgUre4DiweazUbg=; b=xcvz7OBCy3Z8h5Cj3JG4ZGF6GkGSlxjdUoNIoZzO02T51cX6lBBBiFTdBQa5B5j0o8 rW9uIuMAj+EyL4Q9TccZu3CX/+eycn8fkuiCl6PwJyph0m7knsYYVumTv9a6iFQfk+yU DbPTgSzxjaoHAJNAgCAnBWNXDiJP8qH9PM6k4EwDJK9a77uZNBaGbtQGxnt9QfztkXWp QGmgnqUI99SDeqc/2O6kQ0EJHRrK7+Kt6dtblhg1pbgIuSl/VYiO7z/sWQewDDr9o4eQ rJLtQkyaS0BWjcHQ6fXyMEfa5X0Ouhs2sIkoTIyP/8kmEpmDcN17Ld4SIA2Xjz9mF4hC CR5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ek5GJSBK; 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 c6-20020a05640227c600b00461e3e1a628si1613938ede.110.2022.10.24.05.50.48; Mon, 24 Oct 2022 05:51:13 -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=ek5GJSBK; 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 S234910AbiJXMtj (ORCPT + 99 others); Mon, 24 Oct 2022 08:49:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234502AbiJXMo6 (ORCPT ); Mon, 24 Oct 2022 08:44:58 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C68C5140E7; Mon, 24 Oct 2022 05:09:32 -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 2F48A61278; Mon, 24 Oct 2022 12:08:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3CF9CC433C1; Mon, 24 Oct 2022 12:08:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666613292; bh=kwbVD9Us+cXE954oSglAIQMXgHk6yPoSGcch9kkRkps=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ek5GJSBKXp7x9GCKUFLPDn3N4u4MrXRqK2H3Bbfe+0r3FF97JCzlrZT6Bw0GeWGLH l8kSBJp8R34Cf81ExDhgqMb1Fgi3rY1/vXQXn8el75Jurs0WwQQoTsNVhgl9mlWRJO 34ymamJH+FsZ6vgBI7EzTecLjDmZ6I3oiqwQcZqs= 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.4 081/255] sctp: handle the error returned from sctp_auth_asoc_init_active_key Date: Mon, 24 Oct 2022 13:29:51 +0200 Message-Id: <20221024113005.198074246@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113002.471093005@linuxfoundation.org> References: <20221024113002.471093005@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?1747573405824018486?= X-GMAIL-MSGID: =?utf-8?q?1747573405824018486?= 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 3b2d0bd616dd..6b97b734a16f 100644 --- a/net/sctp/auth.c +++ b/net/sctp/auth.c @@ -869,12 +869,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; } @@ -908,8 +913,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;