Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp197518rwi; Wed, 12 Oct 2022 18:32:57 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5KF6vsw6bZBKFJaP6mFXYoG++PV42c0WT9HE6D7O8tkLFe5fBHgXD/Z+vAIHGwgE9+yKFd X-Received: by 2002:a17:90b:4f4d:b0:20d:a08e:3876 with SMTP id pj13-20020a17090b4f4d00b0020da08e3876mr2600206pjb.0.1665624776889; Wed, 12 Oct 2022 18:32:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665624776; cv=none; d=google.com; s=arc-20160816; b=vw9KeVnndpG/ArU1Q9FLnUFGItlYWevf0hBI3sZx1q925cQan9McX0+x8TUiXO7La5 +sni9muqQkhoIRd7mQ+wpcHYWGSRnDR9YxhCvyPillGTpjta3ltfwe1g3scow4m/6Zc/ ow9qT1myFpal58VaRjPZRu56TiErRAMTydEfG9DL7onEHgYCgesy4JUKZK+PZWt/jlVA kWEmCm136Ir1OSPZi1fXqBuaLkGzz+wrfPcDGFTdoAzt5h+MEaycgUvUBlhCc0AhEYpX 2Cww5vsOnC1yDhxlCSfiUK7zBiYpKQlftOMlT/R1TiTaAtK2OWDA2UDloz+lA0TaMYN7 HRww== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=xsMP00KGJLR9ARQTx1Ob3XOPyfYDiVpXdSqFZGjj3u8=; b=EvUnOuQtY/nbsh8D7px7A0HeHqSwOpWBdbfnVH/SnL1IF8VhlrOX24ehsnpqGGNUQG hfwd6cUSWQi1EFpJYba09cl26Zj5BkzGvkp7zor2ejzyDT06RsYJedKqJrlbCj5LIWH0 Hr9bpLklcW3dDZ/kugVCaVlEGXv/GA9hoQRkqeFbDHpLmlbyS/iG+BtU6wQjyAENrHDa VNMoEfOU7JeQj+HmJKBvVOcevsVLKGdZtjWkBa7zFC7oi/GQXlIMc1pmQV5PP6FIB6VT l1TnRjTikKPfaOb9aOM1e0AZ/tEcvZvBI98sUrFMumcmhIXSy35xHnbYYNa/wwC7D009 /ocA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sT59bcQ+; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i17-20020a63d451000000b00462a6f64703si12518727pgj.376.2022.10.12.18.32.45; Wed, 12 Oct 2022 18:32:56 -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=@kernel.org header.s=k20201202 header.b=sT59bcQ+; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232267AbiJMAxO (ORCPT + 99 others); Wed, 12 Oct 2022 20:53:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231598AbiJMAwr (ORCPT ); Wed, 12 Oct 2022 20:52:47 -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 6EDE1748F6; Wed, 12 Oct 2022 17:49:31 -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 DEB8261697; Thu, 13 Oct 2022 00:22:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 327A4C433C1; Thu, 13 Oct 2022 00:22:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665620579; bh=wOSjVS5ufd6jL4mvjWSRUAPxuM5WwHpWoGZeXiaNzDs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sT59bcQ+cvZc/FBsyFSQeJcTiL19fuSNQ7+MTAiFYf7koy1e+SDJy3T/rXCeUY1qt z602PaUCoLK5wOAuVqLbDtfC+E3LIQU2lOkWvlSKSbhdw0jYQl1C++pPiN40vhnkTj /vbqsnA9nBhR+WSr1vAu78RchjHHIig7uDkpzgFeXq/+4MMswYNKpmRVwlulDG7WNw Bnv2so2pa7SpNA79xzliP67uxa8Hwozn+u6h2zKvZcYyu9c6cRAIxQEcCgaWVUWdo7 weF7Z571kr1bC4MgWhzFE/Iw6yXS0aijEx/n77/lQTts0dNqubsLJu9LDHxYAZXoc9 Pv7XwOCKwPCfA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Xiaoke Wang , Greg Kroah-Hartman , Sasha Levin , kushalkothari285@gmail.com, remckee0@gmail.com, namcaov@gmail.com, eng.alaamohamedsoliman.am@gmail.com, jagathjog1996@gmail.com, linux-staging@lists.linux.dev Subject: [PATCH AUTOSEL 5.15 36/47] staging: rtl8723bs: fix a potential memory leak in rtw_init_cmd_priv() Date: Wed, 12 Oct 2022 20:21:11 -0400 Message-Id: <20221013002124.1894077-36-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221013002124.1894077-1-sashal@kernel.org> References: <20221013002124.1894077-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 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: Xiaoke Wang [ Upstream commit 708056fba733a73d926772ea4ce9a42d240345da ] In rtw_init_cmd_priv(), if `pcmdpriv->rsp_allocated_buf` is allocated in failure, then `pcmdpriv->cmd_allocated_buf` will be not properly released. Besides, considering there are only two error paths and the first one can directly return, so we do not need implicitly jump to the `exit` tag to execute the error handler. So this patch added `kfree(pcmdpriv->cmd_allocated_buf);` on the error path to release the resource and simplified the return logic of rtw_init_cmd_priv(). As there is no proper device to test with, no runtime testing was performed. Signed-off-by: Xiaoke Wang Link: https://lore.kernel.org/r/tencent_2B7931B79BA38E22205C5A09EFDF11E48805@qq.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/staging/rtl8723bs/core/rtw_cmd.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/drivers/staging/rtl8723bs/core/rtw_cmd.c b/drivers/staging/rtl8723bs/core/rtw_cmd.c index d494c06dab96..93e3a4c9e115 100644 --- a/drivers/staging/rtl8723bs/core/rtw_cmd.c +++ b/drivers/staging/rtl8723bs/core/rtw_cmd.c @@ -161,8 +161,6 @@ static struct cmd_hdl wlancmds[] = { int rtw_init_cmd_priv(struct cmd_priv *pcmdpriv) { - int res = 0; - init_completion(&pcmdpriv->cmd_queue_comp); init_completion(&pcmdpriv->terminate_cmdthread_comp); @@ -174,18 +172,16 @@ int rtw_init_cmd_priv(struct cmd_priv *pcmdpriv) pcmdpriv->cmd_allocated_buf = rtw_zmalloc(MAX_CMDSZ + CMDBUFF_ALIGN_SZ); - if (!pcmdpriv->cmd_allocated_buf) { - res = -ENOMEM; - goto exit; - } + if (!pcmdpriv->cmd_allocated_buf) + return -ENOMEM; pcmdpriv->cmd_buf = pcmdpriv->cmd_allocated_buf + CMDBUFF_ALIGN_SZ - ((SIZE_PTR)(pcmdpriv->cmd_allocated_buf) & (CMDBUFF_ALIGN_SZ-1)); pcmdpriv->rsp_allocated_buf = rtw_zmalloc(MAX_RSPSZ + 4); if (!pcmdpriv->rsp_allocated_buf) { - res = -ENOMEM; - goto exit; + kfree(pcmdpriv->cmd_allocated_buf); + return -ENOMEM; } pcmdpriv->rsp_buf = pcmdpriv->rsp_allocated_buf + 4 - ((SIZE_PTR)(pcmdpriv->rsp_allocated_buf) & 3); @@ -195,8 +191,8 @@ int rtw_init_cmd_priv(struct cmd_priv *pcmdpriv) pcmdpriv->rsp_cnt = 0; mutex_init(&pcmdpriv->sctx_mutex); -exit: - return res; + + return 0; } static void c2h_wk_callback(struct work_struct *work); -- 2.35.1