From patchwork Mon Oct 24 11:30:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 8736 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp423947wru; Mon, 24 Oct 2022 05:26:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6DiJRP3/HyQWkuHwgHSHLtCsf97q09377oMjD3rB4iNNf08Neq3iNTytFtk+jSYBgHW4M4 X-Received: by 2002:a17:90b:384f:b0:20d:4761:3394 with SMTP id nl15-20020a17090b384f00b0020d47613394mr38821865pjb.144.1666614363499; Mon, 24 Oct 2022 05:26:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666614363; cv=none; d=google.com; s=arc-20160816; b=VMe4Gk3wSsaQ7ZGd2/819rrOrKURn3vwisJLE2w35PrH63ssx5Li2gkOavULSNJmGl bBDD2KVL+UaluLGH6iMv7W25+4JlheWjZv7F/znzePHT0yj/XWpUjEAVB90oip4Xthts rAsqLsqmmGyS6GPIWwUbt4A/tuusSwB9pTJO+D8zjmk2Jcp2nh14T+4IxNO1T/awbVHm 8SEeDqSQNrm9VHdzIuIgsxDE3CgeIOFU9+PtqNGV2d5nEBqPXHaTk60aOdOOSkkAc87M +5x/wzfonKz6XuGEy4j2PayQMq2ZCdnl5ub7BaSnb90bdkvzbNmWTNboNFslPOGYKgCJ ziFA== 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=Dwgs+791h4gx7U0GaiJvswbpC0uSa6D7p3UILa5leJY=; b=khe8ttoI59tJ8AYgYhmoqFE6/BAC3xOIt3NmvU3nAz75HnW2Hqaib7XFpjC8piwCKD VSYUZRYIjk/3SwfxKiPJonfjJ+zmctlimYa6wrKkCHizO30BMd7oKA1ogIk7R5n8xmGY PYndAcmragADY9J76q/TEAxnhliuQkmC223LEtZFLbQD/gYYEf6Mdzlekwi/NvlU5IFj lBmkxDFRA+eE0d46ps5fSwIHH/WRtghNJ9BNWlJc4DUhG2hsP3vB0LAsUJSejhRhTNpt lbG6oTYtM5AwlWn2bSFV1qS3VvTaabq4i/C94YdKpK+3ZEIh+XG0TWXcpDe8LZEqT+aH yEtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=e+GGKs9Y; 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 b67-20020a636746000000b0046a1ae37df5si34216075pgc.147.2022.10.24.05.25.50; Mon, 24 Oct 2022 05:26:03 -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=e+GGKs9Y; 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 S233608AbiJXMZW (ORCPT + 99 others); Mon, 24 Oct 2022 08:25:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233549AbiJXMXs (ORCPT ); Mon, 24 Oct 2022 08:23:48 -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 887C6237ED; Mon, 24 Oct 2022 04:59:48 -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 BC43D61252; Mon, 24 Oct 2022 11:59:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE7DBC433D6; Mon, 24 Oct 2022 11:59:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666612788; bh=OYkkBGYFwO3XqTSXEnulGFuXbO6MZt4+eTDcMl83zT4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=e+GGKs9YeUZRtEMf8DLY5nmad7yt+S6SMTyierEy/FJYDk+wd8gpN4Mc8RXuv11Gz a0dOQh6XvSaB2ZhvKqmUnUrvCwfORInSF51nBCpMFO5rW4a6AGqM9ppn681njlNiwy Ag8I6MFC1jd205J72pgjRkuP6xyMCO66rK7dfvTs= 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 4.19 088/229] sctp: handle the error returned from sctp_auth_asoc_init_active_key Date: Mon, 24 Oct 2022 13:30:07 +0200 Message-Id: <20221024113001.902489012@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024112959.085534368@linuxfoundation.org> References: <20221024112959.085534368@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?1747571822508428141?= X-GMAIL-MSGID: =?utf-8?q?1747571822508428141?= 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 9e0c98df20da..9cf61a18098a 100644 --- a/net/sctp/auth.c +++ b/net/sctp/auth.c @@ -886,12 +886,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; } @@ -920,8 +925,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;