Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1942156ybt; Sun, 21 Jun 2020 04:22:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxpEq2oj2OKz3HSa+6pa9HwWqm2ad1Y+TZBhB8r30xrHbyisKjSDtUr5LnPdzLZj1xQWBRQ X-Received: by 2002:a17:906:2c08:: with SMTP id e8mr11243387ejh.385.1592738538638; Sun, 21 Jun 2020 04:22:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592738538; cv=none; d=google.com; s=arc-20160816; b=P9YnPObMKc4rGA1WEiefxxkiwS6bLCceiMOrO8SH17uPX6ORGSe6DBPQ1YVFjmnPA+ mpllHA+/NbF+KhDv7CixddvXnSbfACoV7e5mqMF/IfomyXU8ogyWMRATScvgIVFY6CAE goYKI88IMx33e85AhBf9z9kkfmzWOhWLGti2v9tzVaOrHOcl5auLcBKmI+pXSpVrtofx bgDSQkaKYDGpsKEEKxVMdIZ0/DTanmhDFeELZwWpRK6cMX+LCygM87u6RMta5OO7NLjP kbjxQbwpoFFJ2CgFBfD7fC6ApfIhX+ipKeStmcJC1KkGNpXJQN44/c6AHvazYSUr5496 NLSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=XItUOgtDkSC8N2WME8ORXj4VI644vnWrjLBjNfSJsjY=; b=YcVTnMLK4uXBg6BRRDtDJwKpfrDD25Iz0ulQk4W2eoaj8bzlhDSwO/IAfirVjzoutb mPDjpXcMMvFat9BxBPztzfnGrKpW+iqZrQraWoKa6LfFzns6uB0tzlTICeMmZD5KZaj9 SLMDLF6eLgp15Y60MHLfUchpGZyedE7noywUOgYR/RRvTcBaohLPzGAq4K4P9mBTuYYY zJ2wJxqm4IKYJ39wUe0bZCd1ASid+woRIFh4g2nbFNtfl5PvriAULhsYnxeCvfd+m1jN voFBqG36DSJB8qKpWL8Nu98pV75HcOpKn638ROGNBBxT3paYgOT2vziiDfLpT+Av7GW2 vN8A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bz17si1080071ejc.222.2020.06.21.04.21.56; Sun, 21 Jun 2020 04:22:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729979AbgFULUL (ORCPT + 99 others); Sun, 21 Jun 2020 07:20:11 -0400 Received: from foss.arm.com ([217.140.110.172]:58554 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729878AbgFULUK (ORCPT ); Sun, 21 Jun 2020 07:20:10 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 47D3A101E; Sun, 21 Jun 2020 04:20:09 -0700 (PDT) Received: from e110176-lin.arm.com (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id DFEA03F6CF; Sun, 21 Jun 2020 04:20:07 -0700 (PDT) From: Gilad Ben-Yossef To: Herbert Xu , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Ofir Drang , Markus Elfring , "David S. Miller" Subject: [PATCH v2 1/3] crypto: ccree: fix resource leak on error path Date: Sun, 21 Jun 2020 14:19:57 +0300 Message-Id: <20200621112000.31495-2-gilad@benyossef.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200621112000.31495-1-gilad@benyossef.com> References: <20200621112000.31495-1-gilad@benyossef.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Fix a small resource leak on the error path of cipher processing. Signed-off-by: Gilad Ben-Yossef Fixes: 63ee04c8b491e ("crypto: ccree - add skcipher support") Cc: Markus Elfring --- drivers/crypto/ccree/cc_cipher.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/drivers/crypto/ccree/cc_cipher.c b/drivers/crypto/ccree/cc_cipher.c index 42ec46be427d..c2f759a04625 100644 --- a/drivers/crypto/ccree/cc_cipher.c +++ b/drivers/crypto/ccree/cc_cipher.c @@ -163,7 +163,6 @@ static int cc_cipher_init(struct crypto_tfm *tfm) skcipher_alg.base); struct device *dev = drvdata_to_dev(cc_alg->drvdata); unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize; - int rc = 0; dev_dbg(dev, "Initializing context @%p for %s\n", ctx_p, crypto_tfm_alg_name(tfm)); @@ -175,10 +174,19 @@ static int cc_cipher_init(struct crypto_tfm *tfm) ctx_p->flow_mode = cc_alg->flow_mode; ctx_p->drvdata = cc_alg->drvdata; + if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) { + /* Alloc hash tfm for essiv */ + ctx_p->shash_tfm = crypto_alloc_shash("sha256-generic", 0, 0); + if (IS_ERR(ctx_p->shash_tfm)) { + dev_err(dev, "Error allocating hash tfm for ESSIV.\n"); + return PTR_ERR(ctx_p->shash_tfm); + } + } + /* Allocate key buffer, cache line aligned */ ctx_p->user.key = kmalloc(max_key_buf_size, GFP_KERNEL); if (!ctx_p->user.key) - return -ENOMEM; + goto free_shash; dev_dbg(dev, "Allocated key buffer in context. key=@%p\n", ctx_p->user.key); @@ -190,21 +198,19 @@ static int cc_cipher_init(struct crypto_tfm *tfm) if (dma_mapping_error(dev, ctx_p->user.key_dma_addr)) { dev_err(dev, "Mapping Key %u B at va=%pK for DMA failed\n", max_key_buf_size, ctx_p->user.key); - return -ENOMEM; + goto free_key; } dev_dbg(dev, "Mapped key %u B at va=%pK to dma=%pad\n", max_key_buf_size, ctx_p->user.key, &ctx_p->user.key_dma_addr); - if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) { - /* Alloc hash tfm for essiv */ - ctx_p->shash_tfm = crypto_alloc_shash("sha256-generic", 0, 0); - if (IS_ERR(ctx_p->shash_tfm)) { - dev_err(dev, "Error allocating hash tfm for ESSIV.\n"); - return PTR_ERR(ctx_p->shash_tfm); - } - } + return 0; - return rc; +free_key: + kfree(ctx_p->user.key); +free_shash: + crypto_free_shash(ctx_p->shash_tfm); + + return -ENOMEM; } static void cc_cipher_exit(struct crypto_tfm *tfm) -- 2.27.0