Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp1942950imn; Mon, 1 Aug 2022 05:54:00 -0700 (PDT) X-Google-Smtp-Source: AA6agR7MhrHcXIS697diB+pzCVgxOhnLByGFS/l2dexSK4GiwpEPZxWPk68398bQo81sN7Lbvh4b X-Received: by 2002:a17:902:f650:b0:15f:3a10:a020 with SMTP id m16-20020a170902f65000b0015f3a10a020mr16448342plg.61.1659358439127; Mon, 01 Aug 2022 05:53:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659358439; cv=none; d=google.com; s=arc-20160816; b=Nvez952oia2+yl5zD0JJm1NH5yDc89/seUXA62YtR9ID53roGQKOFu6JfOm7tzkGtu qtZE6g6SS5YkVyuCu+41LpvvJa5vHdHDY2SWUTXUG+qFKBFdd5sGaTKP6YdPlGbRPCz7 mYgbx8K3NYsp+lI+26ONztpM7r+bjjptIn1rvkYDS2UPydqfDg5hvfV5AynvEdAYH1go rLdqVRk9S53pDV/X6YEGR9IuBdywPAdfwu0Kksmag9fVk86uJPkVtdV0zUfMlQZh9wMS Fj6exg0XMivUNujDmh9PWzTgyHNJ4CqAjdW3+DIDU7oy+Wj/jgtSd/H8LyqdiMRw+/PW wneQ== 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=9JylAmITW/n/ODVEFwrv+TrGEw4EALDJxP+UYIVbwKg=; b=j0jw4m5qUrPCTateFm+Oe6SPZ6etzMd8vtayX1RJ5hSshP8gPRmRJspM8jNt2Kkeeh 0VfGYT3TcW5dT3X4ZR8fnW8s8oblOjUMJNX58pJ9gFN3xwb3rmC8Q4InaEicCqAVh7Cs 3CoPslUDiBJ4r4pSin4IaUO3QU+9oNFEBJqJzSLgmIRQPqr6zD455ljvNOmdlX6wjKM0 TaekO5gYgMjokyYmfkSccestZBXxbo8t2kkCl/Q5aytasaNX4ym7FvqcUGnqFT6ODzgI PnrNHEPAKghYjuU1cwBzhCDFcQ8/325BTcXkZgHpfvIJHS/q3L7c8RUp/3v8TGMOhLH5 2kOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xOQqhVXf; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v27-20020a63465b000000b0040df75dfee9si11787458pgk.561.2022.08.01.05.53.44; Mon, 01 Aug 2022 05:53:59 -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=xOQqhVXf; 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 S232528AbiHAL4n (ORCPT + 99 others); Mon, 1 Aug 2022 07:56:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232494AbiHALz1 (ORCPT ); Mon, 1 Aug 2022 07:55:27 -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 D00D545F5B; Mon, 1 Aug 2022 04:51:17 -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 BA5A7B80E8F; Mon, 1 Aug 2022 11:51:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F2B96C433C1; Mon, 1 Aug 2022 11:51:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1659354674; bh=zBdGI8o8H4l7saZFE93kxeJhGBWoit7hYH+sBaWW+kQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xOQqhVXfZyvILrQ/mI2T+sFgBmJvwKk9P+ozx8L18bP0gGUB+r2sIrdl8bvQRd7P/ ckWt1d4A6HrRAj0oeAA6fPfTx1+DWVvvLPBHBAlS1tNrluq9B669ukjUh0H/PFS5gZ eSdz15EPe3MVEXEwOpqePXWHMOAh6aCAa6cDE/MU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Wei Chen , Xin Long , Jakub Kicinski , Sasha Levin Subject: [PATCH 5.10 46/65] sctp: leave the err path free in sctp_stream_init to sctp_stream_free Date: Mon, 1 Aug 2022 13:47:03 +0200 Message-Id: <20220801114135.658956418@linuxfoundation.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220801114133.641770326@linuxfoundation.org> References: <20220801114133.641770326@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 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 From: Xin Long [ Upstream commit 181d8d2066c000ba0a0e6940a7ad80f1a0e68e9d ] A NULL pointer dereference was reported by Wei Chen: BUG: kernel NULL pointer dereference, address: 0000000000000000 RIP: 0010:__list_del_entry_valid+0x26/0x80 Call Trace: sctp_sched_dequeue_common+0x1c/0x90 sctp_sched_prio_dequeue+0x67/0x80 __sctp_outq_teardown+0x299/0x380 sctp_outq_free+0x15/0x20 sctp_association_free+0xc3/0x440 sctp_do_sm+0x1ca7/0x2210 sctp_assoc_bh_rcv+0x1f6/0x340 This happens when calling sctp_sendmsg without connecting to server first. In this case, a data chunk already queues up in send queue of client side when processing the INIT_ACK from server in sctp_process_init() where it calls sctp_stream_init() to alloc stream_in. If it fails to alloc stream_in all stream_out will be freed in sctp_stream_init's err path. Then in the asoc freeing it will crash when dequeuing this data chunk as stream_out is missing. As we can't free stream out before dequeuing all data from send queue, and this patch is to fix it by moving the err path stream_out/in freeing in sctp_stream_init() to sctp_stream_free() which is eventually called when freeing the asoc in sctp_association_free(). This fix also makes the code in sctp_process_init() more clear. Note that in sctp_association_init() when it fails in sctp_stream_init(), sctp_association_free() will not be called, and in that case it should go to 'stream_free' err path to free stream instead of 'fail_init'. Fixes: 5bbbbe32a431 ("sctp: introduce stream scheduler foundations") Reported-by: Wei Chen Signed-off-by: Xin Long Link: https://lore.kernel.org/r/831a3dc100c4908ff76e5bcc363be97f2778bc0b.1658787066.git.lucien.xin@gmail.com Signed-off-by: Jakub Kicinski Signed-off-by: Sasha Levin --- net/sctp/associola.c | 5 ++--- net/sctp/stream.c | 19 +++---------------- 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/net/sctp/associola.c b/net/sctp/associola.c index fdb69d46276d..2d4ec6187755 100644 --- a/net/sctp/associola.c +++ b/net/sctp/associola.c @@ -226,9 +226,8 @@ static struct sctp_association *sctp_association_init( if (!sctp_ulpq_init(&asoc->ulpq, asoc)) goto fail_init; - if (sctp_stream_init(&asoc->stream, asoc->c.sinit_num_ostreams, - 0, gfp)) - goto fail_init; + if (sctp_stream_init(&asoc->stream, asoc->c.sinit_num_ostreams, 0, gfp)) + goto stream_free; /* Initialize default path MTU. */ asoc->pathmtu = sp->pathmtu; diff --git a/net/sctp/stream.c b/net/sctp/stream.c index 6dc95dcc0ff4..ef9fceadef8d 100644 --- a/net/sctp/stream.c +++ b/net/sctp/stream.c @@ -137,7 +137,7 @@ int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt, ret = sctp_stream_alloc_out(stream, outcnt, gfp); if (ret) - goto out_err; + return ret; for (i = 0; i < stream->outcnt; i++) SCTP_SO(stream, i)->state = SCTP_STREAM_OPEN; @@ -145,22 +145,9 @@ int sctp_stream_init(struct sctp_stream *stream, __u16 outcnt, __u16 incnt, handle_in: sctp_stream_interleave_init(stream); if (!incnt) - goto out; - - ret = sctp_stream_alloc_in(stream, incnt, gfp); - if (ret) - goto in_err; - - goto out; + return 0; -in_err: - sched->free(stream); - genradix_free(&stream->in); -out_err: - genradix_free(&stream->out); - stream->outcnt = 0; -out: - return ret; + return sctp_stream_alloc_in(stream, incnt, gfp); } int sctp_stream_init_ext(struct sctp_stream *stream, __u16 sid) -- 2.35.1