Received: by 2002:a05:6a10:c7c6:0:0:0:0 with SMTP id h6csp2188303pxy; Mon, 2 Aug 2021 23:07:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz5RsHajgXgsx7kJgfiAlp391LLCpBnb32zfBVzUH+n9LKkehSB0DcPgIKRhwN57Cspb6d8 X-Received: by 2002:a17:906:3c04:: with SMTP id h4mr18572795ejg.89.1627970869628; Mon, 02 Aug 2021 23:07:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627970869; cv=none; d=google.com; s=arc-20160816; b=DPhakxh4xTGPxhsArJY5z1eG1nNAMI9zy1UQsY8RayrVKI5gKuaKTH6bhXRtHvmQkX mGPovjqqFBVHpYvQFmxC6I8IVyCHZZQVzOu2ziguy0xG5+2lTPwduiUuilltdosN1G2l sy+P9RnpVp55aY5AESvm3b1D37Wy+JBj0eCnpi1DNzdeMeOZsGxSDSlZXPpLb0a1B6jI 0+EUDZqHnOYc08tCE4EauZe+VYvlJdGe/clqizy8P32EkIiFPLzC96vujrNcihjGW9I+ Rv1fO2X55KoC3AZRNAoBNhAltNOGtSsQ8QTyLNesPA6gL/kKSNY0rcxNDwcNqJ38HQX8 2ZFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=3r+75DACLv5UUEGAMg/p6dRcOLqZwifCJvbUYocHdso=; b=ENgOnHQB548aaXIfaK1ISNQ2zq+OC/D2E5RIGuwfizG9QdlOAVSUhqMVkvihIbhr0d JS3Yilr0gVCJuBZ4845VAbl6YnNI3mKR7FaS8pn0Yd8wHtJlJEAxpKKzv96DkLxLpMEb u3AVrsbCPd/yqKz3lNrytPoyfbFbPq1TT3OOZNGQFbUE8Bcqf04/KHn74wGAg5RrWfaX bUfVH0gi6vtW3C29JzkyB10M5zTde9Nicx4lttx3wDYWKDEN6ZTh4lWcC5FpK5vYdPWF LyBKzu5oKEBFqd+m24VCCHeu5rBjQvBXlLiDDH2OGjO4BJxDkrp8BRz/aBl7M386IS+8 YMHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=t+VGVJ3C; 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 ci9si11589544ejb.255.2021.08.02.23.07.25; Mon, 02 Aug 2021 23:07:49 -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=t+VGVJ3C; 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 S231386AbhHCGHW (ORCPT + 99 others); Tue, 3 Aug 2021 02:07:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230096AbhHCGHW (ORCPT ); Tue, 3 Aug 2021 02:07:22 -0400 Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF749C061764; Mon, 2 Aug 2021 23:07:10 -0700 (PDT) Received: by mail-ej1-x636.google.com with SMTP id go31so34545064ejc.6; Mon, 02 Aug 2021 23:07:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=3r+75DACLv5UUEGAMg/p6dRcOLqZwifCJvbUYocHdso=; b=t+VGVJ3CQj5LZ3tfmjte2QwHY0kpdQJXZpNvVg5efqsDIbP8CP6JmmPAb3HSAiVIqt c6+VIpY9Pd3FSLaoiKepJwlnYQeglAmEf+6DCUDal6qhQfsPCx6Lpk3FciGHa9R7SBHj x3b0h5H1lR5IKiCyBEyM/eHgXLomeO3olR/61dCBBtpXy1Iv+KEyolcmMB/WWLvZraln 5QGI6HP0yXFyCmP+0N7ptO0CPavxi8C0LSwy2pZmn2a7gMesutvlT6FUCdWClTeZ2I7Y RdDeZZFLHOHfXGtrRm3bMrUvutphD4m4sD4IDfOL95L8YKN2y/jWX6LRU+45oXY0UL+S 7ggA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=3r+75DACLv5UUEGAMg/p6dRcOLqZwifCJvbUYocHdso=; b=mnBmDuTLwLhnrAtM7F52ZRR3AuKvevEro/3pw7DzNO85m6d9uQ8HEiOj2TAqeD3xnV xEe22wXoumUIsRL8f4m2/mP8MjiOXFXiB+BsItoOd1NR3hXN+qp3Cd0Kfx7bkl7FEUm4 gcw930M/gr6ePYBw6uT+3jmlybTcm8/qmGkylu4KEgjl2RUiqtTUS+5TSUxtTATBfb+H m+lcKcBCVel5+jOHLmZUMeQKpoOYKQePI/T5f/iE1rfWrN9ea2/b2lEPXevyTfasRyN5 X5TKZLg8jCrmx7VYEn05glE96GJJVU8/yOKL0y1jZebygYvRizXl9YJzxUVd0TEzjq+w NTgQ== X-Gm-Message-State: AOAM530TTIF7QkSAvww+Pdx0owtWD/HXCqbovcwSlC/5uHm/QVsiK1i/ PJdFi12FFG+swA7topOrrdZLtvcAL2WKnQeBgJM= X-Received: by 2002:a17:906:a890:: with SMTP id ha16mr549039ejb.73.1627970829399; Mon, 02 Aug 2021 23:07:09 -0700 (PDT) MIME-Version: 1.0 References: <20210726152724.2281408-1-mudongliangabcd@gmail.com> In-Reply-To: From: Dongliang Mu Date: Tue, 3 Aug 2021 14:06:43 +0800 Message-ID: Subject: Re: [PATCH] crypto: sun8i-ce: fix multiple memory leaks in sun8i_ce_hash_run To: Corentin Labbe 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Tue, Aug 3, 2021 at 1:57 PM Dongliang Mu wro= te: > > On Tue, Aug 3, 2021 at 2:25 AM Corentin Labbe = wrote: > > > > Le Mon, Jul 26, 2021 at 11:27:12PM +0800, Dongliang Mu a =C3=A9crit : > > > In sun8i_ce_hash_run, all the dma_mmap_sg/single will cause memory le= ak > > > 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). > > Sure. How about "Fix this by freeing the objects allocated by > dma_mmap_sg/single when errors occur."? > > > > > > 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/driv= ers/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 *eng= ine, void *breq) > > > > > > /* the padding could be up to two block. */ > > > buf =3D kzalloc(bs * 2, GFP_KERNEL | GFP_DMA); > > > - if (!buf) { > > > - err =3D -ENOMEM; > > > - goto theend; > > > > Please keep all goto error for being consistent. > > OK, no problem. > > BTW, usually for the 1st malloc failure, I prefer returning with errno di= rectly. For this case, as crypto_finalize_hash_request must be executed, so directly return is incorrect. I will add a goto label before crypto_finalize_hash_request. > > > > > > - } > > > + if (!buf) > > > + return -ENOMEM; > > > bf =3D (__le32 *)buf; > > > > > > result =3D kzalloc(digestsize, GFP_KERNEL | GFP_DMA); > > > if (!result) { > > > - err =3D -ENOMEM; > > > - goto theend; > > > + kfree(buf); > > > + return -ENOMEM; > > > } > > > > > > flow =3D rctx->flow; > > > @@ -321,7 +319,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engin= e, void *breq) > > > if (nr_sgs <=3D 0 || nr_sgs > MAX_SG) { > > > dev_err(ce->dev, "Invalid sg number %d\n", nr_sgs); > > > err =3D -EINVAL; > > > - goto theend; > > > + goto err_result; > > > } > > > > > > len =3D areq->nbytes; > > > @@ -334,7 +332,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engin= e, void *breq) > > > if (len > 0) { > > > dev_err(ce->dev, "remaining len %d\n", len); > > > err =3D -EINVAL; > > > - goto theend; > > > + goto err_unmap_sg; > > > } > > > addr_res =3D dma_map_single(ce->dev, result, digestsize, DMA_FR= OM_DEVICE); > > > cet->t_dst[0].addr =3D cpu_to_le32(addr_res); > > > @@ -342,7 +340,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engin= e, void *breq) > > > if (dma_mapping_error(ce->dev, addr_res)) { > > > dev_err(ce->dev, "DMA map dest\n"); > > > err =3D -EINVAL; > > > - goto theend; > > > + goto err_unmap_sg; > > > } > > > > > > byte_count =3D areq->nbytes; > > > @@ -392,7 +390,7 @@ int sun8i_ce_hash_run(struct crypto_engine *engin= e, void *breq) > > > if (dma_mapping_error(ce->dev, addr_pad)) { > > > dev_err(ce->dev, "DMA error on padding SG\n"); > > > err =3D -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 *eng= ine, void *breq) > > > err =3D sun8i_ce_run_task(ce, flow, crypto_tfm_alg_name(areq->b= ase.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 "i= f (!err)" > > Sure. > > > > > Thanks for your work > > Regards