Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3640783pxv; Mon, 26 Jul 2021 08:32:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDi7CMXjXVjjK8FOQLHJXcJMkxE2FZ39IE64CD6C0IcVIePHCsH99DVwNOInCd6KhdG5Li X-Received: by 2002:aa7:c5c3:: with SMTP id h3mr22557276eds.376.1627313559446; Mon, 26 Jul 2021 08:32:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627313559; cv=none; d=google.com; s=arc-20160816; b=HPZMk84yX3bnmIr4rPkboGFBQHXCEDIoQ1Z9muuUv/aD4nNOeiqJ9d5Cz6Lnf1qQGU dslIsgidcQ/mWhstTJZABL4OvnRbPIZ6hz9fWx22jGzExYhGFyhFWaYawWDtuJ+tDHo7 MLm6IOM21GzL0UlKSR5llrvAgLgDbmFMYZ51Gb4RnWPLVJvBkKtYgeO3WlBgm5ZKPgbK fs2m4CCr3LdU96DYYVxbM6Y4ejgCGz8ufv3bHusPOiQzdzOJt7CJ0DoyVIzWyaRdw8o4 O8o/sG5ZN2OzuzMXukrYInp4iN4Rt19AUNK7FP/tFCM7p2HSUZGWv2BU0IzRkAVtWwiv DNCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=mRghCE9uemL2l6phf1pi3lSsvM3GyyFlNkgm44kAP9E=; b=F22mHFATaA8rqSPyCPyQIlnjYS8vFZqzdqYffKyzsmCprnTKVc4dEbFa338LSWEQXD b5cDTrUesc7py178LzDy/KBlmgVniroGb9PkkPb43vYztEkVzLHVjUFUCoNgt9qHgyvc whUt6MZCPLV8sbWuILOBjpLtWA1nTwRRuxOdncJMSoFFFtLbebFYNgtWXiH0rBkLuAOH Ax7cZbS2vVAWSV/SQtl1cWBgDGr6NnZkxblq6c6Qky0XOVkYKcX/pftUEs7atvtXHj1D ibUvFjaAHNVIgbdckiKdCPeedbqjuakgnaJgcnuwV3B5zVaeHapI55z84K47zkazeW+J 2xVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=bsj2JvMF; 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 b16si212213ejv.305.2021.07.26.08.32.15; Mon, 26 Jul 2021 08:32:39 -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=bsj2JvMF; 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 S234766AbhGZOvh (ORCPT + 99 others); Mon, 26 Jul 2021 10:51:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234546AbhGZOve (ORCPT ); Mon, 26 Jul 2021 10:51:34 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E61B0C061757; Mon, 26 Jul 2021 08:32:01 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id j2so10239189edp.11; Mon, 26 Jul 2021 08:32:01 -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; bh=mRghCE9uemL2l6phf1pi3lSsvM3GyyFlNkgm44kAP9E=; b=bsj2JvMFZcXilYHqaNJDghFt4dIqQ5yUNZWk14BP7QVHElB8WKAtCoq7yjraGnyOGn xt+zF2xe3CokN5POWc0EOoDlHYmKF2in0V88WqyAlVEen7Apo/t9jpa3V5VDmVN9EgjJ TPCgLXb5DSVV6TK9XaYXPytiM2tJuWsLU7lAGCP7K5NYiSRdmxi2EvWwUm0ktBh8NfTi Pr2TNYl9LpyUIJJ+Tq5o9Pkj/RtQuhbQ+SBGd1rmbjWqmD4sVBArhRBxGdD10cPHSlew GyVFw43Euf9xk7k0oy8nruhzf53RjsmGLR5Wh7Z9dzCe4LjRu6sjWImi01+LcAMShGpl k47A== 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; bh=mRghCE9uemL2l6phf1pi3lSsvM3GyyFlNkgm44kAP9E=; b=OptRrovm/0BESVEa5y60OswvmR+HDC05QjKDGWL8X3a3MFO1GfZQWZOaEI+NT2pcEn cpKR5w9uplEEAt1uyJtfhumWsrJtrRZn4d6lbkC/aZ2MYAjxVJ/9TT/plNzAYp32ZVn4 EKgIdMSboCI8s1yPmnseZL6zIznqnyoDQFyTlv68Rfj/Bpc+40rHaC95psx93/X+BbRm ofPKhU6SlinWqBN3WFKhBZHkftbNcJ6LPi/dB7Chq5EO1/jgpEYC2XVc1kqCcmdT1DI/ XG4q/pprTr1ExF+Pa7znmQFYIIvvSZTDkcSDMoRhX3LbT948aNh/6Qg996yr4Y/qm3A+ PBRw== X-Gm-Message-State: AOAM532EAyE3A6xcG7xk+p6bNqtkfRK27ljvwK9lvbDgrN9qFH5Yz8hE gzkD/Ikq6h3tpfhOf8my8VZvGvSC9ucha+Ed2t4= X-Received: by 2002:a05:6402:270d:: with SMTP id y13mr22349779edd.66.1627313520540; Mon, 26 Jul 2021 08:32:00 -0700 (PDT) MIME-Version: 1.0 References: <20210726152533.2281139-1-mudongliangabcd@gmail.com> In-Reply-To: <20210726152533.2281139-1-mudongliangabcd@gmail.com> From: Dongliang Mu Date: Mon, 26 Jul 2021 23:31:34 +0800 Message-ID: Subject: Re: [PATCH v4] crypto: sun8i-ss: fix multiple memory leaks in sun8i_ss_hash_run To: Corentin Labbe , Herbert Xu , "David S. Miller" , Maxime Ripard , Chen-Yu Tsai , Jernej Skrabec , Ard Biesheuvel , Xiang Chen , Eric Biggers , Dongliang Mu , Colin Ian King Cc: 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" Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org On Mon, Jul 26, 2021 at 11:25 PM Dongliang Mu wrote: > > In sun8i_ss_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. > > Fixes: d9b45418a917 ("crypto: sun8i-ss - support hash algorithms") > Signed-off-by: Dongliang Mu > --- > v1->v2: move crypto_finalize_hash_request to the end of function; move > the memcpy after the dma_mmap_sg/single functions. > v2->v3: remove some unrelated code changes; delete the fix of return value > since there is no corresponding handling code > v3->v4: change sun8i_ce to sun8i_ss From v3 to v4, I changed the title and commit message from sun8i_ce to sun8i_ss because I suddenly realized that the code change below is for sun8i_ss_hash_run, other than sun8i_ce_hash_run. Furthermore, sun8i_ce is also prone to this issue. I've sent another patch to fix it. > drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 15 ++++++++------- > 1 file changed, 8 insertions(+), 7 deletions(-) > > diff --git a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c > index 3c073eb3db03..5448705e8ae1 100644 > --- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c > +++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c > @@ -368,14 +368,14 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) > if (nr_sgs <= 0 || nr_sgs > MAX_SG) { > dev_err(ss->dev, "Invalid sg number %d\n", nr_sgs); > err = -EINVAL; > - goto theend; > + goto err_result; > } > > addr_res = dma_map_single(ss->dev, result, digestsize, DMA_FROM_DEVICE); > if (dma_mapping_error(ss->dev, addr_res)) { > dev_err(ss->dev, "DMA map dest\n"); > err = -EINVAL; > - goto theend; > + goto err_unmap_sg; > } > > len = areq->nbytes; > @@ -390,7 +390,7 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) > if (len > 0) { > dev_err(ss->dev, "remaining len %d\n", len); > err = -EINVAL; > - goto theend; > + goto err_addr_res; > } > > byte_count = areq->nbytes; > @@ -428,18 +428,19 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) > if (dma_mapping_error(ss->dev, addr_pad)) { > dev_err(ss->dev, "DMA error on padding SG\n"); > err = -EINVAL; > - goto theend; > + goto err_addr_res; > } > > err = sun8i_ss_run_hash_task(ss, rctx, crypto_tfm_alg_name(areq->base.tfm)); > > dma_unmap_single(ss->dev, addr_pad, j * 4, DMA_TO_DEVICE); > +err_addr_res: > + dma_unmap_single(ss->dev, addr_res, digestsize, DMA_FROM_DEVICE); > +err_unmap_sg: > dma_unmap_sg(ss->dev, areq->src, sg_nents(areq->src), > DMA_TO_DEVICE); > - dma_unmap_single(ss->dev, addr_res, digestsize, DMA_FROM_DEVICE); > - > memcpy(areq->result, result, algt->alg.hash.halg.digestsize); > -theend: > +err_result: > kfree(pad); > kfree(result); > crypto_finalize_hash_request(engine, breq, err); > -- > 2.25.1 >