[PATCH] drivers/crypto/ccp/ccp-ops.c: Fix a crash due to incorrect cleanup usage of kfree

Ella Ma posted 1 patch 1 month ago
drivers/crypto/ccp/ccp-ops.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] drivers/crypto/ccp/ccp-ops.c: Fix a crash due to incorrect cleanup usage of kfree
Posted by Ella Ma 1 month ago
Annotating a local pointer variable, which will be assigned with the
kmalloc-family functions, with the `__cleanup(kfree)` attribute will
make the address of the local variable, rather than the address returned
by kmalloc, passed to kfree directly and lead to a crash due to invalid
deallocation of stack address. According to other places in the repo,
the correct usage should be `__free(kfree)`. The code coincidentally
compiled because the parameter type `void *` of kfree is compatible with
the desired type `struct { ... } **`.

Fixes: a71475582ada ("crypto: ccp - reduce stack usage in ccp_run_aes_gcm_cmd")
Signed-off-by: Ella Ma <alansnape3058@gmail.com>
---

I don't have the machine to actually test the changed place. So I tried
locally with a simple test module. The crash happens right when the
module is being loaded.

```C
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int __init custom_init(void) {
  printk(KERN_INFO "Crash reproduce for drivers/crypto/ccp/ccp-ops.c");
  int *p __cleanup(kfree) = kzalloc(sizeof(int), GFP_KERNEL);
  *p = 42;
  return 0;
}
static void __exit custom_exit(void) {}
module_init(custom_init);
module_exit(custom_exit);
```

BESIDES, scripts/checkpatch.pl reports a coding style issue originally
existing in the code, `sizeof *wa`, I fixed this together in this patch.

 drivers/crypto/ccp/ccp-ops.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/crypto/ccp/ccp-ops.c b/drivers/crypto/ccp/ccp-ops.c
index d78865d9d5f0..f80a92006666 100644
--- a/drivers/crypto/ccp/ccp-ops.c
+++ b/drivers/crypto/ccp/ccp-ops.c
@@ -642,7 +642,7 @@ ccp_run_aes_gcm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 		struct ccp_data dst;
 		struct ccp_data aad;
 		struct ccp_op op;
-	} *wa __cleanup(kfree) = kzalloc(sizeof *wa, GFP_KERNEL);
+	} *wa __free(kfree) = kzalloc(sizeof(*wa), GFP_KERNEL);
 	unsigned int dm_offset;
 	unsigned int authsize;
 	unsigned int jobid;
-- 
2.34.1
Re: [PATCH] drivers/crypto/ccp/ccp-ops.c: Fix a crash due to incorrect cleanup usage of kfree
Posted by Tom Lendacky 1 month ago
On 1/8/26 09:29, Ella Ma wrote:
> Annotating a local pointer variable, which will be assigned with the
> kmalloc-family functions, with the `__cleanup(kfree)` attribute will
> make the address of the local variable, rather than the address returned
> by kmalloc, passed to kfree directly and lead to a crash due to invalid
> deallocation of stack address. According to other places in the repo,
> the correct usage should be `__free(kfree)`. The code coincidentally
> compiled because the parameter type `void *` of kfree is compatible with
> the desired type `struct { ... } **`.
> 
> Fixes: a71475582ada ("crypto: ccp - reduce stack usage in ccp_run_aes_gcm_cmd")
> Signed-off-by: Ella Ma <alansnape3058@gmail.com>

As was pointed out by Markus, subject should be "crypto: ccp - Fix a ... "

Otherwise,

Acked-by: Tom Lendacky <thomas.lendacky@gmail.com>

> ---
> 
> I don't have the machine to actually test the changed place. So I tried
> locally with a simple test module. The crash happens right when the
> module is being loaded.
> 
> ```C
> #include <linux/init.h>
> #include <linux/module.h>
> MODULE_LICENSE("GPL");
> static int __init custom_init(void) {
>   printk(KERN_INFO "Crash reproduce for drivers/crypto/ccp/ccp-ops.c");
>   int *p __cleanup(kfree) = kzalloc(sizeof(int), GFP_KERNEL);
>   *p = 42;
>   return 0;
> }
> static void __exit custom_exit(void) {}
> module_init(custom_init);
> module_exit(custom_exit);
> ```
> 
> BESIDES, scripts/checkpatch.pl reports a coding style issue originally
> existing in the code, `sizeof *wa`, I fixed this together in this patch.
> 
>  drivers/crypto/ccp/ccp-ops.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/crypto/ccp/ccp-ops.c b/drivers/crypto/ccp/ccp-ops.c
> index d78865d9d5f0..f80a92006666 100644
> --- a/drivers/crypto/ccp/ccp-ops.c
> +++ b/drivers/crypto/ccp/ccp-ops.c
> @@ -642,7 +642,7 @@ ccp_run_aes_gcm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
>  		struct ccp_data dst;
>  		struct ccp_data aad;
>  		struct ccp_op op;
> -	} *wa __cleanup(kfree) = kzalloc(sizeof *wa, GFP_KERNEL);
> +	} *wa __free(kfree) = kzalloc(sizeof(*wa), GFP_KERNEL);
>  	unsigned int dm_offset;
>  	unsigned int authsize;
>  	unsigned int jobid;
Re: [PATCH] drivers/crypto/ccp/ccp-ops.c: Fix a crash due to incorrect cleanup usage of kfree
Posted by Markus Elfring 1 month ago
> Annotating a local pointer variable, which will be assigned with the
> kmalloc-family functions, with the `__cleanup(kfree)` attribute will
…

Would an other subsystem specification be preferred for the patch subject?
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/log/drivers/crypto/ccp/ccp-ops.c

Regards,
Markus
[PATCH v2] crypto: ccp - Fix a crash due to incorrect cleanup usage of kfree
Posted by Ella Ma 4 weeks, 1 day ago
Annotating a local pointer variable, which will be assigned with the
kmalloc-family functions, with the `__cleanup(kfree)` attribute will
make the address of the local variable, rather than the address returned
by kmalloc, passed to kfree directly and lead to a crash due to invalid
deallocation of stack address. According to other places in the repo,
the correct usage should be `__free(kfree)`. The code coincidentally
compiled because the parameter type `void *` of kfree is compatible with
the desired type `struct { ... } **`.

Fixes: a71475582ada ("crypto: ccp - reduce stack usage in ccp_run_aes_gcm_cmd")
Signed-off-by: Ella Ma <alansnape3058@gmail.com>
Acked-by: Tom Lendacky <thomas.lendacky@gmail.com>
---

Changes in v2:
- Update the subject prefix as suggested by Markus


I don't have the machine to actually test the changed place. So I tried
locally with a simple test module. The crash happens right when the
module is being loaded.

```C
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int __init custom_init(void) {
  printk(KERN_INFO "Crash reproduce for drivers/crypto/ccp/ccp-ops.c");
  int *p __cleanup(kfree) = kzalloc(sizeof(int), GFP_KERNEL);
  *p = 42;
  return 0;
}
static void __exit custom_exit(void) {}
module_init(custom_init);
module_exit(custom_exit);
```

BESIDES, scripts/checkpatch.pl reports a coding style issue originally
existing in the code, `sizeof *wa`, I fixed this together in this patch.

 drivers/crypto/ccp/ccp-ops.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/crypto/ccp/ccp-ops.c b/drivers/crypto/ccp/ccp-ops.c
index d78865d9d5f0..f80a92006666 100644
--- a/drivers/crypto/ccp/ccp-ops.c
+++ b/drivers/crypto/ccp/ccp-ops.c
@@ -642,7 +642,7 @@ ccp_run_aes_gcm_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 		struct ccp_data dst;
 		struct ccp_data aad;
 		struct ccp_op op;
-	} *wa __cleanup(kfree) = kzalloc(sizeof *wa, GFP_KERNEL);
+	} *wa __free(kfree) = kzalloc(sizeof(*wa), GFP_KERNEL);
 	unsigned int dm_offset;
 	unsigned int authsize;
 	unsigned int jobid;
-- 
2.34.1
Re: [PATCH v2] crypto: ccp - Fix a crash due to incorrect cleanup usage of kfree
Posted by Joe Perches 1 week ago
On Fri, 2026-01-09 at 16:17 +0100, Ella Ma wrote:
> Annotating a local pointer variable, which will be assigned with the
> kmalloc-family functions, with the `__cleanup(kfree)` attribute will
> make the address of the local variable, rather than the address returned
> by kmalloc, passed to kfree directly and lead to a crash due to invalid
> deallocation of stack address. According to other places in the repo,
> the correct usage should be `__free(kfree)`. The code coincidentally
> compiled because the parameter type `void *` of kfree is compatible with
> the desired type `struct { ... } **`.
> 
> Fixes: a71475582ada ("crypto: ccp - reduce stack usage in ccp_run_aes_gcm_cmd")

Perhaps this one too?

drivers/gpu/drm/xe/xe_guc_ct.c: char *buf __cleanup(kfree) = kmalloc(SZ_4K, GFP_NOWAIT);
Re: [PATCH v2] crypto: ccp - Fix a crash due to incorrect cleanup usage of kfree
Posted by Herbert Xu 1 week ago
On Fri, Jan 09, 2026 at 04:17:24PM +0100, Ella Ma wrote:
> Annotating a local pointer variable, which will be assigned with the
> kmalloc-family functions, with the `__cleanup(kfree)` attribute will
> make the address of the local variable, rather than the address returned
> by kmalloc, passed to kfree directly and lead to a crash due to invalid
> deallocation of stack address. According to other places in the repo,
> the correct usage should be `__free(kfree)`. The code coincidentally
> compiled because the parameter type `void *` of kfree is compatible with
> the desired type `struct { ... } **`.
> 
> Fixes: a71475582ada ("crypto: ccp - reduce stack usage in ccp_run_aes_gcm_cmd")
> Signed-off-by: Ella Ma <alansnape3058@gmail.com>
> Acked-by: Tom Lendacky <thomas.lendacky@gmail.com>
> ---
> 
> Changes in v2:
> - Update the subject prefix as suggested by Markus
> 
> 
> I don't have the machine to actually test the changed place. So I tried
> locally with a simple test module. The crash happens right when the
> module is being loaded.
> 
> ```C
> #include <linux/init.h>
> #include <linux/module.h>
> MODULE_LICENSE("GPL");
> static int __init custom_init(void) {
>   printk(KERN_INFO "Crash reproduce for drivers/crypto/ccp/ccp-ops.c");
>   int *p __cleanup(kfree) = kzalloc(sizeof(int), GFP_KERNEL);
>   *p = 42;
>   return 0;
> }
> static void __exit custom_exit(void) {}
> module_init(custom_init);
> module_exit(custom_exit);
> ```
> 
> BESIDES, scripts/checkpatch.pl reports a coding style issue originally
> existing in the code, `sizeof *wa`, I fixed this together in this patch.
> 
>  drivers/crypto/ccp/ccp-ops.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Patch applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt