Skip to contentSkip to conversation

crypto: nx - fix memory leaks in nx842_crypto_{alloc,free}_ctx

PATCH In review Thorsten Blum 2 participants 3 messages Mar 11

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: nx - fix memory leaks in nx842_crypto_{alloc,free}_ctx

The bounce buffers are allocated with __get_free_pages() using BOUNCE_BUFFER_ORDER (order 2 = 4 pages), but both the allocation error path and nx842_crypto_free_ctx() release the buffers with free_page(). Use free_pages() with the matching order instead.

Also, since the scomp conversion, nx842_crypto_alloc_ctx() allocates the context separately, but nx842_crypto_free_ctx() never releases it. Add the missing kfree(ctx) in nx842_crypto_free_ctx(), and reuse nx842_crypto_free_ctx() in the allocation error path.

Fixes ed70b479c2c0 ("crypto: nx - add hardware 842 crypto comp alg")
Fixes 980b5705f4e7 ("crypto: nx - Migrate to scomp API")
---
 drivers/crypto/nx/nx-842.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/crypto/nx/nx-842.c +4 -6
index b61f2545e165..a61208cbcd27 100644
@@ -115,10 +115,7 @@ void *nx842_crypto_alloc_ctx(struct nx842_driver *driver)
 	ctx->sbounce = (u8 *)__get_free_pages(GFP_KERNEL, BOUNCE_BUFFER_ORDER);
 	ctx->dbounce = (u8 *)__get_free_pages(GFP_KERNEL, BOUNCE_BUFFER_ORDER);
 	if (!ctx->wmem || !ctx->sbounce || !ctx->dbounce) {
-		kfree(ctx->wmem);
-		free_page((unsigned long)ctx->sbounce);
-		free_page((unsigned long)ctx->dbounce);
-		kfree(ctx);
+		nx842_crypto_free_ctx(ctx);
 		return ERR_PTR(-ENOMEM);
 	}
 
@@ -131,8 +128,9 @@ void nx842_crypto_free_ctx(void *p)
 	struct nx842_crypto_ctx *ctx = p;
 
 	kfree(ctx->wmem);
-	free_page((unsigned long)ctx->sbounce);
-	free_page((unsigned long)ctx->dbounce);
+	free_pages((unsigned long)ctx->sbounce, BOUNCE_BUFFER_ORDER);
+	free_pages((unsigned long)ctx->dbounce, BOUNCE_BUFFER_ORDER);
+	kfree(ctx);
 }
 EXPORT_SYMBOL_GPL(nx842_crypto_free_ctx);
 
#1 lore raw

Hi Thorsten,

Quoted 5 lines · Thorsten Blum

The bounce buffers are allocated with __get_free_pages() using BOUNCE_BUFFER_ORDER (order 2 = 4 pages), but both the allocation error path and nx842_crypto_free_ctx() release the buffers with free_page(). Use free_pages() with the matching order instead.

Quoted 7 lines

Also, since the scomp conversion, nx842_crypto_alloc_ctx() allocates the context separately, but nx842_crypto_free_ctx() never releases it. Add the missing kfree(ctx) in nx842_crypto_free_ctx(), and reuse nx842_crypto_free_ctx() in the allocation error path.

Fixes ed70b479c2c0 ("crypto: nx - add hardware 842 crypto comp alg")
Fixes 980b5705f4e7 ("crypto: nx - Migrate to scomp API")

Thanks for the fixes.

Given that you are fixing two separate issues that were introduced ~10 years apart, I think it would be better to split this up.

#2 lore raw
Quoted 11 lines · Ard Biesheuvel
Quoted 10 lines · Thorsten Blum

The bounce buffers are allocated with __get_free_pages() using BOUNCE_BUFFER_ORDER (order 2 = 4 pages), but both the allocation error path and nx842_crypto_free_ctx() release the buffers with free_page(). Use free_pages() with the matching order instead.

Also, since the scomp conversion, nx842_crypto_alloc_ctx() allocates the context separately, but nx842_crypto_free_ctx() never releases it. Add the missing kfree(ctx) in nx842_crypto_free_ctx(), and reuse nx842_crypto_free_ctx() in the allocation error path.

Quoted 6 lines
Quoted 2 lines
Fixes ed70b479c2c0 ("crypto: nx - add hardware 842 crypto comp alg")
Fixes 980b5705f4e7 ("crypto: nx - Migrate to scomp API")

Thanks for the fixes.

Given that you are fixing two separate issues that were introduced ~10 years apart, I think it would be better to split this up.

Yes, good idea. I submitted them separately here:

https://lore.kernel.org/lkml/[email protected]/

Thanks,
Thorsten