Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3319142pxv; Mon, 26 Jul 2021 01:03:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzy1AjvoVzEiFdCqoPT2WTFC1cce7A8Yq6ptJcvQdI1/42f3JAsbYY5XoycO98n2A3Tp6cR X-Received: by 2002:a05:6402:35c1:: with SMTP id z1mr9726135edc.89.1627286616924; Mon, 26 Jul 2021 01:03:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627286616; cv=none; d=google.com; s=arc-20160816; b=aCvDK+oSDsjtjNo/XsHDhcIuLJxDz1ipC6Ucq5Z4C4a26nLnGOzJqg6QvS/Rrv8M2a TttZvtTmlYtYiczaye2d9MdE0M3lzONBAu4TTEyE8UsvvYmV9Mwpg/uypvNXEJ7UCaYb AyfguNnaow5FkW2D5HT9xBLWiAlz/jpF/k9lYwrnY11Ua5tJ9O7qHaw66vEd+oXJdvW2 SUVGILac+zBFvJN4UWrvxV2GslloLM0MAZGizRKv9X3Yd+p94hBKv/xJacX5i1Rup/76 7B+JEd1cI494MpKKSzaaCwwPNWfFIhhenK5UBW8NYNyCPcx+qa6R/ixDxbG1fZeT+AI3 mCLA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=TztOQrqAi1WWdC8KOhyOiu1dKlM5nvZj+oiQ3YMyM5Q=; b=bAkoY+YvQ2MiFa1jacOSd+iZxeC/EqQ+CEe8vk6jSQCrY6UL5SriiFjJxPgTHD9DnM 44u2PTZjl5lU3Xdd47PjNeJdPQ3BVuCshFzGdS7jw7zZVJwlRdyGavDDr7MNk1W2X/FM rbHI2CCZMyd39ex8KQ3Bun/mS5LGTHss3qraBcHeyMH+VlpZrB3fwm67cnXK4YkqFZkh bMqET0e+3vzDONMdO4QvVY2/9FqvTkI7LdaXLvTB7XFCdO7AsADReITVc5TzpToL3pia zVDKgBzcJw4d8loBBxs4rUoh2Jfb4XsTBc0pT4tLPLfic3lbgWnfFFxT5ZyM7sPePv7k /LSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ZGB5+21l; 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 ck11si24068043ejb.397.2021.07.26.01.03.03; Mon, 26 Jul 2021 01:03:36 -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=ZGB5+21l; 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 S232815AbhGZHWW (ORCPT + 99 others); Mon, 26 Jul 2021 03:22:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53066 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232888AbhGZHWT (ORCPT ); Mon, 26 Jul 2021 03:22:19 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CEB03C061757; Mon, 26 Jul 2021 01:02:34 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id c16so5027488plh.7; Mon, 26 Jul 2021 01:02:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=TztOQrqAi1WWdC8KOhyOiu1dKlM5nvZj+oiQ3YMyM5Q=; b=ZGB5+21luOnAKkD/7A35U+OjiJZK9c+9pwkHP9A018dW/F2AD0w9H/5V18yWp1G7UQ wpol1BciVR1o+Q31GnZR6Z3ePGJGKRuflhZxDlO4Te5Gma0D45BhIIlhQbwHPUk3bPtw DabraqMSH/M/kFGi+fnyAlpbyWf/Wmwn7KRsWJdXhTOocQEV5VtKD4VAHD9sovSPSniD 1soqFwySa83YNflTn00pgF0ueAFriFz7BLs2ZfjP41bAl3j1o5X7TivslDQ+izLspg7f dho1VdM9SN+hzLDiJ7sSxtSoK6NCwsNyhrfAt//nDLm25qUv5HQeFQBF6yHdCSZDmU9L sB/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=TztOQrqAi1WWdC8KOhyOiu1dKlM5nvZj+oiQ3YMyM5Q=; b=Qc9srNIoL8UUG7d9pdz9a/gqod4s3dK4VTaJ6UTX1Er8vYPbJ14DW1W0kBKBbRlU10 DBIpl2ZWozkX/OzoJSXSMoRq3dda5uTWiltcqvZTmqBS7dZNxKUtphJGV3vuVUUzfooy cybNtnU2jIbK5Y6wjQdJrrQ0szLAlJhp2gbNSgu4w/32pGNmLm1AfVjBFabSRhsDVNTu WiKvADmBfOhM4eNUjAAQuxNB/6caU+aYqCQ2/+7eRD2bhZ5jc/HY4ktP8C/TwolbnNQ5 ZQIFF383yO2KGlwJavnBfJwLzbxx9P6aKtrO3yivDuQ8l7PiZ81XAzpKmvAobrXZunG8 WGpA== X-Gm-Message-State: AOAM533XVJJKBk2WZ+2sPZ+mijTe2LNUjeeD+3tP+T4Z3PPq/OiO2bIM +Ow1ebyu8OU6CTmFosmwPx8= X-Received: by 2002:a17:902:d916:b029:11e:3249:4a17 with SMTP id c22-20020a170902d916b029011e32494a17mr13666778plz.0.1627286554161; Mon, 26 Jul 2021 01:02:34 -0700 (PDT) Received: from localhost.localdomain ([154.16.166.171]) by smtp.gmail.com with ESMTPSA id 78sm34049460pfw.189.2021.07.26.01.02.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jul 2021 01:02:33 -0700 (PDT) From: Dongliang Mu To: Corentin Labbe , Herbert Xu , "David S. Miller" , Maxime Ripard , Chen-Yu Tsai , Jernej Skrabec , "Jason A. Donenfeld" , Eric Biggers , Colin Ian King , Dongliang Mu , Xiang Chen Cc: Ard Biesheuvel , linux-crypto@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2] crypto: sun8i-ce: fix memory leak and return value of sun8i_ce_hash_run Date: Mon, 26 Jul 2021 15:59:36 +0800 Message-Id: <20210726075957.2231287-1-mudongliangabcd@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org This patch fixes some memory leak caused by dma_mmap_sg/single in the error handling code. In addition, it fixes the return value when errors related with dma_mmap_sg/single occur. Reported-by: Dongliang Mu Fixes: 732b764099f65 ("crypto: sun8i-ce - fix two error path's memory leak") 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. .../crypto/allwinner/sun8i-ss/sun8i-ss-hash.c | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 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..fe5db3c84754 100644 --- a/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c +++ b/drivers/crypto/allwinner/sun8i-ss/sun8i-ss-hash.c @@ -324,11 +324,10 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) struct sun8i_ss_alg_template *algt; struct sun8i_ss_dev *ss; struct scatterlist *sg; - int nr_sgs, err, digestsize; + int j, i, todo, nr_sgs, digestsize, err; unsigned int len; u64 fill, min_fill, byte_count; void *pad, *result; - int j, i, todo; __be64 *bebits; __le64 *lebits; dma_addr_t addr_res, addr_pad; @@ -368,14 +367,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 +389,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; @@ -421,27 +420,28 @@ int sun8i_ss_hash_run(struct crypto_engine *engine, void *breq) } addr_pad = dma_map_single(ss->dev, pad, j * 4, DMA_TO_DEVICE); - rctx->t_src[i].addr = addr_pad; - rctx->t_src[i].len = j; - rctx->t_dst[i].addr = addr_res; - rctx->t_dst[i].len = digestsize / 4; 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; } + rctx->t_src[i].addr = addr_pad; + rctx->t_src[i].len = j; + rctx->t_dst[i].addr = addr_res; + rctx->t_dst[i].len = digestsize / 4; 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); - return 0; + return err; } -- 2.25.1