Skip to content

Commit e09b2a7

Browse files
Gilad Ben-Yossefgregkh
Gilad Ben-Yossef
authored andcommitted
crypto: ccree - fix resource leak on error path
[ Upstream commit 9bc6165 ] Fix a small resource leak on the error path of cipher processing. Signed-off-by: Gilad Ben-Yossef <[email protected]> Fixes: 63ee04c ("crypto: ccree - add skcipher support") Cc: Markus Elfring <[email protected]> Signed-off-by: Herbert Xu <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 3b17121 commit e09b2a7

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

drivers/crypto/ccree/cc_cipher.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ static int cc_cipher_init(struct crypto_tfm *tfm)
137137
skcipher_alg.base);
138138
struct device *dev = drvdata_to_dev(cc_alg->drvdata);
139139
unsigned int max_key_buf_size = cc_alg->skcipher_alg.max_keysize;
140-
int rc = 0;
141140

142141
dev_dbg(dev, "Initializing context @%p for %s\n", ctx_p,
143142
crypto_tfm_alg_name(tfm));
@@ -149,10 +148,19 @@ static int cc_cipher_init(struct crypto_tfm *tfm)
149148
ctx_p->flow_mode = cc_alg->flow_mode;
150149
ctx_p->drvdata = cc_alg->drvdata;
151150

151+
if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
152+
/* Alloc hash tfm for essiv */
153+
ctx_p->shash_tfm = crypto_alloc_shash("sha256-generic", 0, 0);
154+
if (IS_ERR(ctx_p->shash_tfm)) {
155+
dev_err(dev, "Error allocating hash tfm for ESSIV.\n");
156+
return PTR_ERR(ctx_p->shash_tfm);
157+
}
158+
}
159+
152160
/* Allocate key buffer, cache line aligned */
153161
ctx_p->user.key = kmalloc(max_key_buf_size, GFP_KERNEL);
154162
if (!ctx_p->user.key)
155-
return -ENOMEM;
163+
goto free_shash;
156164

157165
dev_dbg(dev, "Allocated key buffer in context. key=@%p\n",
158166
ctx_p->user.key);
@@ -164,21 +172,19 @@ static int cc_cipher_init(struct crypto_tfm *tfm)
164172
if (dma_mapping_error(dev, ctx_p->user.key_dma_addr)) {
165173
dev_err(dev, "Mapping Key %u B at va=%pK for DMA failed\n",
166174
max_key_buf_size, ctx_p->user.key);
167-
return -ENOMEM;
175+
goto free_key;
168176
}
169177
dev_dbg(dev, "Mapped key %u B at va=%pK to dma=%pad\n",
170178
max_key_buf_size, ctx_p->user.key, &ctx_p->user.key_dma_addr);
171179

172-
if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
173-
/* Alloc hash tfm for essiv */
174-
ctx_p->shash_tfm = crypto_alloc_shash("sha256-generic", 0, 0);
175-
if (IS_ERR(ctx_p->shash_tfm)) {
176-
dev_err(dev, "Error allocating hash tfm for ESSIV.\n");
177-
return PTR_ERR(ctx_p->shash_tfm);
178-
}
179-
}
180+
return 0;
180181

181-
return rc;
182+
free_key:
183+
kfree(ctx_p->user.key);
184+
free_shash:
185+
crypto_free_shash(ctx_p->shash_tfm);
186+
187+
return -ENOMEM;
182188
}
183189

184190
static void cc_cipher_exit(struct crypto_tfm *tfm)

0 commit comments

Comments
 (0)