Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp1835895pxy; Mon, 2 Aug 2021 11:26:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDLoj5OJAgy/ZNZkYnm2lTwYqDVjXufCrgJ9ei5QRGL8IJ6/UvjYqyR+JMY6eWURWhh4Ah X-Received: by 2002:aa7:c7d0:: with SMTP id o16mr20801852eds.75.1627928791720; Mon, 02 Aug 2021 11:26:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627928791; cv=none; d=google.com; s=arc-20160816; b=erz8DQIUaPfUFapkzY3nfc94kaTlghpXpFJbHsEp+LyQozhkUMQN0SMGONW4ZUfF5P mH0Y88oAbKV0+wvBFE31bl4zsIVpIogKdE9fOj9dQ8dj7slsyZK9iVQjEwtmk8Q2GEhE qsdqYMujIVKu8uUpGqb28eMNCpb2BupmhxhtTvPjO+WNKTVqd24s6KAmV/WtYJDlRabS SQyTJfz5yvnVQICCrrzcAkDxCJzwD7KhC9wZ5+3DUYnEad0fpUuxcBtD9sanvOjhpa70 dLIf5CqB/CvttAaDdrUmveQo0Zth5i/DTQc8+//xUUltwjiqxJ4MGuUWIbMnawx/ElYw xx3w== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=2PmZEzitR5QCFfVulNWIgxV1Tcx/s/ZuM62LoiFh3Nw=; b=Il/3EhSTNWxKhFRszeIh1hTFVrI3Iw9c3b1KFx/IHCW4jWw8Gi9i5h0badu+HpARzh pN+yaMFSsgMQpl1FejWEuQ98Hzaa3+RV7RK5BpwNPOkn5OEFq69yFEc0+xlDroDdeadn jMdBooRyrL9yNtU877jtgKWiCtVJA9Ghu/ht81UXgTALvhkZ8v8rX4+WCKZRTW/+62de 1xZe3Piwgn+O/KfMPLY0vXOQ4NEqIsw4XjYVG+M5x+JLrCKBRu419Kgaeh8pxgsBvC4Z ifm9Laz2h2KX8bbiDosk/jzXobr/znmNS9uEP53vtif6qoxowj6x2PoRWT8PBnJKxyz5 MRFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=t9WFOobW; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id k9si10086644edh.461.2021.08.02.11.25.54; Mon, 02 Aug 2021 11:26:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=t9WFOobW; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-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 S229571AbhHBSZn (ORCPT + 99 others); Mon, 2 Aug 2021 14:25:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229607AbhHBSZn (ORCPT ); Mon, 2 Aug 2021 14:25:43 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1B849C06175F; Mon, 2 Aug 2021 11:25:32 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id c9so2253292wri.8; Mon, 02 Aug 2021 11:25:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=2PmZEzitR5QCFfVulNWIgxV1Tcx/s/ZuM62LoiFh3Nw=; b=t9WFOobW+5UA5d2UYmSatS4axpFhxEATNH3Fn4mwpxah92vkxPkZa7vAw0Di16hDaR jI9y03eCpaf5jbH40U4DdtWJxmP9aKD/OlfWStyS78uh0mDN3BOomJDP5/uyV39rzdJL cDCV0UZ1eM4eca9j95z5Aznn+OvE7TPIH6sSqB0afAOijc6HIsuBc1t1HZXiyn8AE71z iMm/4zq4T8ryUO+f9afiTI38ftuXPwQFCx6joFnbdkq4EiS7aBAGccu1tPyuwe4y5NU+ QqN1iNTiFB2J9JrOenHXj4cX+8+860Lr78SC0YzMLXXzHGQ3RLjz/44TM0ttaoUpn0LK bVCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=2PmZEzitR5QCFfVulNWIgxV1Tcx/s/ZuM62LoiFh3Nw=; b=DTLASbLnPFKdxFtG9xbDUmMy/NHtuivLmeIMsJ6fo8obD0ERBzjSvsmRhKvESMn5vd qhzk0QljirNbc2A5giCKpw2TsodQf4BrqzlAndQ3QmylAYUy75x5Zkiu0I1cP3IrAa4e lyd++UFBMTM4iFqYhGV9j6ew+MPYN8nXXVXrkOUoCF3yzimomG/PflYKsVqffpIddhaz y6U6r8UBLsrDwk9IFjKRzWNd/gPIqpWIz1R2Sn4oNw2aFjUZE4JMXhxMk0lm3r51eDzz ErC4emJf1OxfjkRPIuNWfpo/QuZfeOoIN4oRuKsPwtoavTmFN6KUWqJGsxw5OrZvWQm+ MQww== X-Gm-Message-State: AOAM533N1XNDCOjJ9fv8wrfFNnVgmp+rkloMTJayNYl7O1TO6663Uilr dShbdoBetMVtz3ZyrptYsiA= X-Received: by 2002:adf:fd90:: with SMTP id d16mr20056591wrr.105.1627928730671; Mon, 02 Aug 2021 11:25:30 -0700 (PDT) Received: from Red ([2a01:cb1d:3d5:a100:264b:feff:fe03:2806]) by smtp.googlemail.com with ESMTPSA id z12sm10663448wml.18.2021.08.02.11.25.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Aug 2021 11:25:29 -0700 (PDT) Date: Mon, 2 Aug 2021 20:25:25 +0200 From: Corentin Labbe To: Dongliang Mu Cc: Herbert Xu , "David S. Miller" , Maxime Ripard , Chen-Yu Tsai , Jernej Skrabec , Ard Biesheuvel , Jonathan Corbet , Eric Biggers , Xiang Chen , Mauro Carvalho Chehab , Corentin Labbe , "Jason A. Donenfeld" , linux-crypto@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org Subject: Re: [PATCH] crypto: sun8i-ce: fix multiple memory leaks in sun8i_ce_hash_run Message-ID: References: <20210726152724.2281408-1-mudongliangabcd@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20210726152724.2281408-1-mudongliangabcd@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Le Mon, Jul 26, 2021 at 11:27:12PM +0800, Dongliang Mu a ?crit : > In sun8i_ce_hash_run, all the dma_mmap_sg/single will cause memory leak > due to no corresponding unmap operation if errors happen. > > Fix this by adding error handling part for all the dma_mmap_sg/single. > I think it could be better worded, error handling is already there (but bad). > Fixes: 56f6d5aee88d ("crypto: sun8i-ce - support hash algorithms") > Signed-off-by: Dongliang Mu > --- > .../crypto/allwinner/sun8i-ce/sun8i-ce-hash.c | 28 +++++++++---------- > 1 file changed, 13 insertions(+), 15 deletions(-) > > diff --git a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c > index 88194718a806..d454ad99deee 100644 > --- a/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c > +++ b/drivers/crypto/allwinner/sun8i-ce/sun8i-ce-hash.c > @@ -286,16 +286,14 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq) > > /* the padding could be up to two block. */ > buf = kzalloc(bs * 2, GFP_KERNEL | GFP_DMA); > - if (!buf) { > - err = -ENOMEM; > - goto theend; Please keep all goto error for being consistent. > - } > + if (!buf) > + return -ENOMEM; > bf = (__le32 *)buf; > > result = kzalloc(digestsize, GFP_KERNEL | GFP_DMA); > if (!result) { > - err = -ENOMEM; > - goto theend; > + kfree(buf); > + return -ENOMEM; > } > > flow = rctx->flow; > @@ -321,7 +319,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq) > if (nr_sgs <= 0 || nr_sgs > MAX_SG) { > dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs); > err = -EINVAL; > - goto theend; > + goto err_result; > } > > len = areq->nbytes; > @@ -334,7 +332,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq) > if (len > 0) { > dev_err(ce->dev, "remaining len %d\n", len); > err = -EINVAL; > - goto theend; > + goto err_unmap_sg; > } > addr_res = dma_map_single(ce->dev, result, digestsize, DMA_FROM_DEVICE); > cet->t_dst[0].addr = cpu_to_le32(addr_res); > @@ -342,7 +340,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq) > if (dma_mapping_error(ce->dev, addr_res)) { > dev_err(ce->dev, "DMA map dest\n"); > err = -EINVAL; > - goto theend; > + goto err_unmap_sg; > } > > byte_count = areq->nbytes; > @@ -392,7 +390,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq) > if (dma_mapping_error(ce->dev, addr_pad)) { > dev_err(ce->dev, "DMA error on padding SG\n"); > err = -EINVAL; > - goto theend; > + goto err_addr_res; > } > > if (ce->variant->hash_t_dlen_in_bits) > @@ -405,15 +403,15 @@ int sun8i_ce_hash_run(struct crypto_engine *engine, void *breq) > err = sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(areq->base.tfm)); > > dma_unmap_single(ce->dev, addr_pad, j * 4, DMA_TO_DEVICE); > +err_addr_res: > + dma_unmap_single(ce->dev, addr_res, digestsize, DMA_FROM_DEVICE); > +err_unmap_sg: > dma_unmap_sg(ce->dev, areq->src, sg_nents(areq->src), > DMA_TO_DEVICE); > - dma_unmap_single(ce->dev, addr_res, digestsize, DMA_FROM_DEVICE); > - > - > memcpy(areq->result, result, algt->alg.hash.halg.digestsize); The result should be copied only when everything is ok. Please add a "if (!err)" Thanks for your work Regards