Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp7019134rwn; Tue, 13 Sep 2022 12:27:14 -0700 (PDT) X-Google-Smtp-Source: AA6agR61Xq5SaeNmC7ewpavVwIaMYvDIVjcmdJ0UM+JgPkveXHxtGTXJ4PpuqliSg+V34dBFYpCK X-Received: by 2002:a17:907:1622:b0:770:7fc1:2921 with SMTP id hb34-20020a170907162200b007707fc12921mr23175140ejc.119.1663097234157; Tue, 13 Sep 2022 12:27:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663097234; cv=none; d=google.com; s=arc-20160816; b=lnp3gZGJHbCSDK96rC8wE0g4/syxqxPk7mo1YEuFvxzhpBRBI0/ytsj5f+tMbjbR1f qSWOHw8qV/5eeGPyy4XNYqtkALffKitkN48dX+ftAibuC1mZkEMu6SXPMvS/z2TvMJek GH7GWib8vlVsOWnpQEZ/cGNVgxp7CvpkQnGXqmlqEa+8SbPXYRfTM416cS4dI1/SyyHG hgnbpaHYQxKr84h91vr9iNDlYMyCrEPRgVHuzIfVMC7Ki7an8v2uSfUhmF+Js1Yj6Ktt e3sJAW+4vZ17O34SRjKd92qbEk97150/1PaFqy0ybIII1k93X6ZQNck1fHnIoRvxXFUF hACA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=3BKjsumQ8PtZhoAH3wD+jnO31UagiaH9PNpiRZwb564=; b=LAnCqCFM5Cd49nEljRFwBzJ/UUnTK7BovW2+wWtJBp0m5QzEcXQU3DY7qivKGW2rhB btCO/RRePi4FLS3GdlXUwY6Skcdvm7qkHW+RU80mjHyp+Ue3ue9xuX9k7NhP8mJ5qOfx jcTcx2lIhVgtDESvUi3OgLHj+I89KkPtI/gbyg2BbBUP7tzbgNS9GlXdWuQPR9bK7BSG 3N+wWIdYNNY3ZfyETzTrh1HJvH9EUSpU2o+juP6pUoGBLwomg5xSJSkSxoWRsdfLi2WQ NZov1+eEEXGHvc8JO80O7zFaSUB0eg8DiS/duYlX5ec3t5tnTw5eaQJpOnObGJQ1xark lgjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=kDK2HYpz; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gs38-20020a1709072d2600b007420947aa83si12640259ejc.193.2022.09.13.12.26.47; Tue, 13 Sep 2022 12:27:14 -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=@gmail.com header.s=20210112 header.b=kDK2HYpz; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231838AbiIMTOk (ORCPT + 99 others); Tue, 13 Sep 2022 15:14:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231614AbiIMTOh (ORCPT ); Tue, 13 Sep 2022 15:14:37 -0400 Received: from mail-ej1-x633.google.com (mail-ej1-x633.google.com [IPv6:2a00:1450:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E96DC10549 for ; Tue, 13 Sep 2022 12:14:35 -0700 (PDT) Received: by mail-ej1-x633.google.com with SMTP id gh9so29797272ejc.8 for ; Tue, 13 Sep 2022 12:14:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=3BKjsumQ8PtZhoAH3wD+jnO31UagiaH9PNpiRZwb564=; b=kDK2HYpzWhIuokBAxXuR7doJoqzUmdb0JgO62oaM2YyYpTYjj6pSXKTEFf5R80TCwY B6i0EUSnUEAumOY8m/GGZZ0fHQHQpMdJnwVuJbacf6FQHYjFiiW7Yn8tUawz5Kv9ARIt BqKUUOWHCsy0vO6i/W/280bwGXCThouhQjt9GzalYhCq764wfr70QZJc0Em7a/d683L+ zl0/4gb2Wud+SwROYX6l00eGCRraBpikLTo2Tth89YQIo0+ICZkhS9leTWJqEcQcW+t8 f33Dcdg+k4uzG3QPFf7b7AQ2NpW/L+mTCo7OvgSfijU8LTiXRjS1vwpDmvXGmVcHmdHw e3fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=3BKjsumQ8PtZhoAH3wD+jnO31UagiaH9PNpiRZwb564=; b=15Byc/aeyNVY6SHXtXCPxF4ZuL2hN47kn3h+tNmBoXdcGsraJ3UBuKCDjQ0wOQJcWx 2H6UmqZJe9j3rhQ9MlUiEZdpDv/DSRj0WYpAuJVEcuOPH3eX4VHEuWXu3WmkOG0H1rwG NNPHGuxXro7QuiACGB8bX7wm9LOHobb/jQSMqquH0rc1hu9hJnhOo1m9PkDyKAZVg6bx nYJeMV2hNRYd4USsiMpUvbjEsbU6AbzIUTuuoPs9YPY2ltwtCiZpZS62o8bG3KufDNxL d0Be1NXjLOtv4/DK1W4xGd+T3wg4kj2VwnbY5l5XbOleeC9XpJ7IH67f9eLfMgZA/d4Y vPxQ== X-Gm-Message-State: ACgBeo3g3V/+RGgZ6/oj6lfERo4igrhP/QKAeZB+gHm4ubKz+yBgtKGR Nq0Hbs+grIWGLoWPOdNupuU= X-Received: by 2002:a17:907:2da1:b0:773:dc01:877a with SMTP id gt33-20020a1709072da100b00773dc01877amr19075731ejc.567.1663096474384; Tue, 13 Sep 2022 12:14:34 -0700 (PDT) Received: from matrix-ESPRIMO-P710 (p57ba2cf5.dip0.t-ipconnect.de. [87.186.44.245]) by smtp.gmail.com with ESMTPSA id pw13-20020a17090720ad00b0073ddb2eff27sm6540846ejb.167.2022.09.13.12.14.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Sep 2022 12:14:34 -0700 (PDT) Date: Tue, 13 Sep 2022 21:14:32 +0200 From: philipp hortmann To: xkernel.wang@foxmail.com Cc: Larry.Finger@lwfinger.net, phil@philpotter.co.uk, gregkh@linuxfoundation.org, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: [PATCH v6] staging: r8188eu: fix a potential memory leak in rtw_init_cmd_priv() Message-ID: <20220913191432.GA11048@matrix-ESPRIMO-P710> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 On Fri, Sep 09, 2022 at 07:45:57PM +0800, xkernel.wang@foxmail.com wrote: > From: Xiaoke Wang > > In rtw_init_cmd_priv(), if `pcmdpriv->rsp_allocated_buf` is allocated > in failure, then `pcmdpriv->cmd_allocated_buf` will not be properly > released. Besides, considering there are only two error paths and the > first one can directly return, we do not need to implicitly jump to the > `exit` tag to execute the error handling code. > > 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 > --- > ChangeLog: > v1->v2: simplify the function. > v2->v3: rebase and recover some whitespace cleanups. > v3->v4: update the description. > v4->v5: rebase... > v5->v6: update the description. > drivers/staging/r8188eu/core/rtw_cmd.c | 15 +++++---------- > 1 file changed, 5 insertions(+), 10 deletions(-) > > diff --git a/drivers/staging/r8188eu/core/rtw_cmd.c b/drivers/staging/r8188eu/core/rtw_cmd.c > index 9c700ff..e38a826 100644 > --- a/drivers/staging/r8188eu/core/rtw_cmd.c > +++ b/drivers/staging/r8188eu/core/rtw_cmd.c > @@ -58,8 +58,6 @@ static int _rtw_enqueue_cmd(struct __queue *queue, struct cmd_obj *obj) > > u32 rtw_init_cmd_priv(struct cmd_priv *pcmdpriv) > { > - u32 res = _SUCCESS; > - > init_completion(&pcmdpriv->enqueue_cmd); > /* sema_init(&(pcmdpriv->cmd_done_sema), 0); */ > init_completion(&pcmdpriv->start_cmd_thread); > @@ -74,27 +72,24 @@ u32 rtw_init_cmd_priv(struct cmd_priv *pcmdpriv) > pcmdpriv->cmd_allocated_buf = kzalloc(MAX_CMDSZ + CMDBUFF_ALIGN_SZ, > GFP_KERNEL); > > - if (!pcmdpriv->cmd_allocated_buf) { > - res = _FAIL; > - goto exit; > - } > + if (!pcmdpriv->cmd_allocated_buf) > + return _FAIL; > > pcmdpriv->cmd_buf = pcmdpriv->cmd_allocated_buf + CMDBUFF_ALIGN_SZ - ((size_t)(pcmdpriv->cmd_allocated_buf) & (CMDBUFF_ALIGN_SZ - 1)); > > pcmdpriv->rsp_allocated_buf = kzalloc(MAX_RSPSZ + 4, GFP_KERNEL); > > if (!pcmdpriv->rsp_allocated_buf) { > - res = _FAIL; > - goto exit; > + kfree(pcmdpriv->cmd_allocated_buf); > + return _FAIL; > } > > pcmdpriv->rsp_buf = pcmdpriv->rsp_allocated_buf + 4 - ((size_t)(pcmdpriv->rsp_allocated_buf) & 3); > > pcmdpriv->cmd_done_cnt = 0; > pcmdpriv->rsp_cnt = 0; > -exit: > > - return res; > + return _SUCCESS; > } > > u32 rtw_init_evt_priv(struct evt_priv *pevtpriv) > -- Tested-by: Philipp Hortmann # Edimax N150