Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3636135pxv; Mon, 26 Jul 2021 08:27:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxYcmhzLFbcWOmQuYVsCfjiUzZQsIELxos4AWFXqtZga2hSs6kbwvXTHXNN3FgHlk+Ykz/l X-Received: by 2002:a92:d412:: with SMTP id q18mr9146914ilm.77.1627313245680; Mon, 26 Jul 2021 08:27:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1627313245; cv=none; d=google.com; s=arc-20160816; b=blkNNh7LQmJsblvbFG3N3ky/di+2oOTYWHQiGaeda3yA5DbozWNjKCc6HbuVv6MCKV HqP7CaoGz8LieASiFIuFv4Vm2olmy27oOvQx1rYvInLLwz9NB16u994mHR0Wr9M/AE0h 32FFa5GlzxPI8LtIrF40NOdygRbXq76JR1p8rSY5yV5q4W8EbRNnVbL7MkoRsNZ7K1C+ LYu6ze8q18MIwFhZUBWfP3HdFL0P1Pe615dY5XJWcdLrv1gYIrFuMlF4iZaNuVleaE6B anjuFoK42+sEmu7jPtM7Yoj2mHfT3n/1LU05HlCQLLL28HPXtNak511DvgV01PqG6WE5 A0Lw== 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=YJ2wiacers242b03J1VtREz0S4EVBe3CkaiVWqJ9kVg=; b=vC7+FT8MVygfTPL2VaKDRhLXlmaiyeFy3dRtKFgyjyb7+HP36Q50gOUKCfYI5Fy8M0 J9PnUU/gp9u+D4dbIgO3IHO2dYpbES51bcH3+SD+KbY4vVySHQgOvDwvsilziKSQGOR6 Nwlp+Xl4s+/RxdHaKozw5by+0A+1OWUxL2QzhK8mGIjf96SKrFvKkYme8CUE2dZfRfo+ slOeAZhM/EHP1LN5G6XR2Rfk5UxUF01DSlSyvulMHrxpONkx1LLXWK9+WfaTg4Uftaag 8Q1LsdLJR/nwPzxFgSgLvlQeFEi47mI9DfsGKdu0Fc8Y1ZdbhLQ9OKhrlkhWRuUPP+Dd aXMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=hXQPIflR; 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 l3si246128iln.90.2021.07.26.08.27.05; Mon, 26 Jul 2021 08:27:25 -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=hXQPIflR; 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 S235110AbhGZOpV (ORCPT + 99 others); Mon, 26 Jul 2021 10:45:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234823AbhGZOpU (ORCPT ); Mon, 26 Jul 2021 10:45:20 -0400 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6DEB7C061757; Mon, 26 Jul 2021 08:25:48 -0700 (PDT) Received: by mail-pl1-x630.google.com with SMTP id a20so12258651plm.0; Mon, 26 Jul 2021 08:25:48 -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=YJ2wiacers242b03J1VtREz0S4EVBe3CkaiVWqJ9kVg=; b=hXQPIflR8ZhFX0feKPijERYg/69KAJPHtc16bQXnx5lGj90ised38lKMXuA3isoLjo Fb50Xww5eEsA1wFlnsHLSziCczZE1Jagx9Y8YNEElKJ9qPA88sVm8K8dfT0dnWRS5BBa aKK94+bXmAgSdIIza7k3+8Q8vrqK9rKc9OgqYH99Irpn8FwFP0USqQxAefNFiJKtKq4F zJw47BcDpvQR/nqjoY9oFLkizYEnsh3ERrDltT3NvLQp2Ma9Bo5Da4ob/Pwme5++Q4ZM Zju/5+OCgF0Po3B1EDRpC/L+DU14X7eIbPkPGtbo5VxH3GW7U79ANkpwjL3SAgv1qHnB VkRw== 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=YJ2wiacers242b03J1VtREz0S4EVBe3CkaiVWqJ9kVg=; b=g5Zo1hf3Y11Cu9sLfjQ7xxrPgtiGJtytUmKXPB8ypxr1xG8BUTJ6X+kCkWXHshqEDX HIeAGrxBy0aXhqpn735HoiHec3hwjnhQWX9fqYNibo6Wg0FtQAOSf6CS04+C+nQJhqx1 XcMdOjPFdyCKO1fpgG0+pymzGaGWHCPOQarHpFkrnmp7ZWej4K7McTK6s7UVOprvQIz9 PMk6zOLFYmQsZODeTRTrYDSQOntCXrpJcaPGcNwymOt9iRkU2omG1GVVCmJxjJAcAjds dZCj8x5NXvGv8bwgGjrJ6TGrjIjfc0F61SaMtepMIYuVVnMkUAGHJSLb8uBnt59K0jEy K/ZA== X-Gm-Message-State: AOAM530KsU6lGxy4NIeNXEjAxCcSj+7UiVelT8zz7RHJjGl7cKcW/rOu OX6h5IjSStbIx97B6dImGg0= X-Received: by 2002:aa7:9541:0:b029:32c:cefa:123f with SMTP id w1-20020aa795410000b029032ccefa123fmr18103572pfq.24.1627313147752; Mon, 26 Jul 2021 08:25:47 -0700 (PDT) Received: from localhost.localdomain ([154.16.166.171]) by smtp.gmail.com with ESMTPSA id x14sm307079pfu.200.2021.07.26.08.25.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jul 2021 08:25:47 -0700 (PDT) From: Dongliang Mu 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@vger.kernel.org Subject: [PATCH v4] crypto: sun8i-ss: fix multiple memory leaks in sun8i_ss_hash_run Date: Mon, 26 Jul 2021 23:25:21 +0800 Message-Id: <20210726152533.2281139-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 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 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