Received: by 2002:a05:6a10:83d0:0:0:0:0 with SMTP id o16csp208601pxh; Thu, 7 Apr 2022 19:05:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxa40sEkOfuYQ4Ut3dOhq+80Hz0rbAGmiszaX7tLAC+0m5IjD7q23htkwAkJUyJlLM5+uSO X-Received: by 2002:a63:981a:0:b0:398:49ba:a65e with SMTP id q26-20020a63981a000000b0039849baa65emr13571756pgd.231.1649383545900; Thu, 07 Apr 2022 19:05:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649383545; cv=none; d=google.com; s=arc-20160816; b=xCujui+eMOvWX0mljkm2LP6aVVw9IyZtu/NmDcSNoiF3B2b7+gN9CnY0Ua1/ddMWKW XNYjTitzKPGX31kCoGgKdE+Q/k0ySYT7p/2Zib3G2vztvxhSZi17W0zc/UIYU3HE46dE CM1EbSg9IGNW2eZRCofuEaGdGthyRUOzs3l8DnVSiXWTGv9AeR2PN223ysdrPeTt4Caq Uc1VxAu7fvvryHgjbUVW2eTrVollHTSRibt/EzNZ0dnkkrF6rJhGiI0TTBVxqv0aOrnE 0hNAEPMLwypPilw7QnflWwiAt5dBzexcBsZNI6ExNbGV2gVUKX/Ncy0aXnytfvdZxQyJ YDxQ== 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:date :subject:cc:to:from:message-id:dkim-signature; bh=AGpgReuc8vUqZuul0VY4Gnt/NTcYGzZUFJEPxGHjf6g=; b=GzJmCTAZs2qkdL8pH2uk1hKvylJHrHu4jnWQrh7TIQDX5H8bfT1mng1c54fUAuj073 uHcu7Uau+3F++qGppTY+FIpc9ymR2AeKoR4b/bK7kYUolfKHJ4d5mPognKMIHcxXNc/I 0i2J6yvBPZAkAcXvstGN5sZfV3c7YwpviefzdE7UQBqlkjwWmPL7dhv+RS7ozH6YjlTR lcR4pj5koiUPuLGlCcRr56IPFrI16u39iM/G1KV34hWrzqNCeEV4xIvmHakXIr9ZubUA u3uVrd/2nQ+dqBEC3sKkxF1CDdinulAKW4jJG5O3XWYBOjLxYNo6bYBvF0zEW4DFN70K +WBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foxmail.com header.s=s201512 header.b=Wx3ainhs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id t6-20020a170902e84600b00153b2d165b1si1479589plg.441.2022.04.07.19.05.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 19:05:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@foxmail.com header.s=s201512 header.b=Wx3ainhs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=foxmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E748EE0AD5; Thu, 7 Apr 2022 18:37:48 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233489AbiDHBjo (ORCPT + 99 others); Thu, 7 Apr 2022 21:39:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50038 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233418AbiDHBjl (ORCPT ); Thu, 7 Apr 2022 21:39:41 -0400 Received: from out203-205-221-155.mail.qq.com (out203-205-221-155.mail.qq.com [203.205.221.155]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A85431403DB for ; Thu, 7 Apr 2022 18:37:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1649381852; bh=AGpgReuc8vUqZuul0VY4Gnt/NTcYGzZUFJEPxGHjf6g=; h=From:To:Cc:Subject:Date; b=Wx3ainhsRe1F0qit34WgopLVnqyGVS7lv0i5yhPdlPQfrMi+VoS1f2TtbcAuK8zwW JWxPADLGfJcVkItK5yjosu0paer8Z63r6GCcdQFRagQLUnJG2MvtqU0Q+uYNBe+GoC aILLzxcjG2rpO6QQtloGjdy+6Qw3Tb0RG9/SsS+o= Received: from localhost.localdomain ([218.197.153.188]) by newxmesmtplogicsvrszc8.qq.com (NewEsmtp) with SMTP id 959184B6; Fri, 08 Apr 2022 09:37:25 +0800 X-QQ-mid: xmsmtpt1649381845t2pbyfq9s Message-ID: X-QQ-XMAILINFO: MYvQTNt/YNi0Ht22sifMJZkDJEOTdZIkWvUEKnmsmcd2NKjpw0fqa/FI4yhq4H V72SOGMMMrh5aPyK7YASGZPHMyBBuqUxxsV/xgReH+dnfG08tsolvXutT+2ADisXvwfCdCpSOHeu RBBKl3ABpVkugU8s6MTYd5VQ2wnFOSNDKsJK/t6eE9A6rSOabWGtuoo4rg7xfPsmBgoDmOnjEDZw LASiW6pLiHvvAJHdSvaal4rtrQJCuReqDYzwupgfOUESHf6eKVUv+DaRqJiWybYjKgi1ou2j9TKA 9ImC56nnCmWvyRrAGw/ZL2mna5S0g83HVcHMMRawCQuSVuA8ge0jarzybp87deJxxBg3jeqnDLfy F8oePinmw1/vDf3Ok3P1H6JhWKlR0b6Gk46BBlGHHpEd3iOhI+2k715BGf2bltQhGgSg4yI3Sn2S ziCeikjyOmpFmCZfgU8sO3JQzpBRDXvkv6icVwOvZTurpFsbhF18XkCGtbaB6lOAltxUoAcKxrrY mG+z1BMZ/yOWUubYtGr8yMJTNloTB6NMrpZCAm9bYABeqaHelWoBoRhzalVhH06G+/CLQd5QYfPq IfHucj2T6gFTRdHbnofK4E2XLFBuVH8fyjrkJiTUMbX0pTgXTm9sgTuhaKyeq9d7vWkhqaDRkcUe 2x3bwrcoVdn9JTQvf2hCTuiulN0UJuKLv99AGEqXYL4RJcxyvNuXM57yCh2wxg9DEbKvFrPt/9yC fCy38Unt12qt5k0qAjmgWf6mGo3aynMaAwXxuaZ1+NVIcshVN6r2AtlqnJp2ZpKx+3TRc6wKdVdE nJYXV0wHBLA9GhwXKwAas3WxRwdfBBvnJWHwqAZuMUMKj75HSD03j+Yje/9+EYEtHxAAf3r1NvMO jKDoMXT2JIL4pqmoqFB0jXbFALo2XZ4sX0IYHloYUxssBZOWITAfpwAVq40VBGN+Q2H0k6vW2p From: xkernel.wang@foxmail.com To: gregkh@linuxfoundation.org, paskripkin@gmail.com Cc: Larry.Finger@lwfinger.net, phil@philpotter.co.uk, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org, Xiaoke Wang Subject: [PATCH v3 1/2] staging: r8188eu: fix potential memory leak in _rtw_init_xmit_priv() Date: Fri, 8 Apr 2022 09:37:10 +0800 X-OQ-MSGID: <20220408013710.15055-1-xkernel.wang@foxmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=no 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 In _rtw_init_xmit_priv(), there are several error paths for allocation failures just jump to the `exit` section. However, there is no action will be performed, so the allocated resources are not properly released, which leads to various memory leaks. To properly release them, this patch unifies the error handling code and several error handling paths are added. According to the allocation sequence, if the validation fails, it will jump to its corresponding error tag to release the resources. Signed-off-by: Xiaoke Wang --- ChangeLog: v1->v2 update the description and adjust the sequence of patches. v2->v3 None to this patch, but some to another pathch in this series. drivers/staging/r8188eu/core/rtw_xmit.c | 32 ++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/drivers/staging/r8188eu/core/rtw_xmit.c b/drivers/staging/r8188eu/core/rtw_xmit.c index aede8ef..865b2fc 100644 --- a/drivers/staging/r8188eu/core/rtw_xmit.c +++ b/drivers/staging/r8188eu/core/rtw_xmit.c @@ -112,7 +112,7 @@ s32 _rtw_init_xmit_priv(struct xmit_priv *pxmitpriv, struct adapter *padapter) if (!pxmitpriv->pallocated_xmitbuf) { res = _FAIL; - goto exit; + goto free_frame_buf; } pxmitpriv->pxmitbuf = (u8 *)N_BYTE_ALIGMENT((size_t)(pxmitpriv->pallocated_xmitbuf), 4); @@ -134,7 +134,7 @@ s32 _rtw_init_xmit_priv(struct xmit_priv *pxmitpriv, struct adapter *padapter) msleep(10); res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ)); if (res == _FAIL) - goto exit; + goto free_xmitbuf; } pxmitbuf->flags = XMIT_VO_QUEUE; @@ -152,7 +152,7 @@ s32 _rtw_init_xmit_priv(struct xmit_priv *pxmitpriv, struct adapter *padapter) if (!pxmitpriv->pallocated_xmit_extbuf) { res = _FAIL; - goto exit; + goto free_xmitbuf; } pxmitpriv->pxmit_extbuf = (u8 *)N_BYTE_ALIGMENT((size_t)(pxmitpriv->pallocated_xmit_extbuf), 4); @@ -167,10 +167,8 @@ s32 _rtw_init_xmit_priv(struct xmit_priv *pxmitpriv, struct adapter *padapter) pxmitbuf->ext_tag = true; res = rtw_os_xmit_resource_alloc(padapter, pxmitbuf, max_xmit_extbuf_size + XMITBUF_ALIGN_SZ); - if (res == _FAIL) { - res = _FAIL; - goto exit; - } + if (res == _FAIL) + goto free_xmit_extbuf; list_add_tail(&pxmitbuf->list, &pxmitpriv->free_xmit_extbuf_queue.queue); pxmitbuf++; @@ -200,8 +198,26 @@ s32 _rtw_init_xmit_priv(struct xmit_priv *pxmitpriv, struct adapter *padapter) rtl8188eu_init_xmit_priv(padapter); -exit: + return _SUCCESS; +free_xmit_extbuf: + pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmit_extbuf; + while (i-- > 0) { + rtw_os_xmit_resource_free(padapter, pxmitbuf, (max_xmit_extbuf_size + XMITBUF_ALIGN_SZ)); + pxmitbuf++; + } + vfree(pxmitpriv->pallocated_xmit_extbuf); + i = NR_XMITBUFF; +free_xmitbuf: + pxmitbuf = (struct xmit_buf *)pxmitpriv->pxmitbuf; + while (i-- > 0) { + rtw_os_xmit_resource_free(padapter, pxmitbuf, (MAX_XMITBUF_SZ + XMITBUF_ALIGN_SZ)); + pxmitbuf++; + } + vfree(pxmitpriv->pallocated_xmitbuf); +free_frame_buf: + vfree(pxmitpriv->pallocated_frame_buf); +exit: return res; } --