[PATCH] crypto: ccree: Fix use after free in cc_cipher_exit()

Jianglei Nie posted 1 patch 4 years, 3 months ago
drivers/crypto/ccree/cc_cipher.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] crypto: ccree: Fix use after free in cc_cipher_exit()
Posted by Jianglei Nie 4 years, 3 months ago
kfree_sensitive(ctx_p->user.key) will free the ctx_p->user.key. But
ctx_p->user.key is still used in the next line, which will lead to a
use after free.

We can call kfree_sensitive() after dev_dbg() to avoid the uaf.

Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
---
 drivers/crypto/ccree/cc_cipher.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/crypto/ccree/cc_cipher.c b/drivers/crypto/ccree/cc_cipher.c
index 78833491f534..309da6334a0a 100644
--- a/drivers/crypto/ccree/cc_cipher.c
+++ b/drivers/crypto/ccree/cc_cipher.c
@@ -257,8 +257,8 @@ static void cc_cipher_exit(struct crypto_tfm *tfm)
 		&ctx_p->user.key_dma_addr);
 
 	/* Free key buffer in context */
-	kfree_sensitive(ctx_p->user.key);
 	dev_dbg(dev, "Free key buffer in context. key=@%p\n", ctx_p->user.key);
+	kfree_sensitive(ctx_p->user.key);
 }
 
 struct tdes_keys {
-- 
2.25.1
Re: [PATCH] crypto: ccree: Fix use after free in cc_cipher_exit()
Posted by Herbert Xu 4 years, 3 months ago
On Thu, Mar 03, 2022 at 10:23:37AM +0800, Jianglei Nie wrote:
> kfree_sensitive(ctx_p->user.key) will free the ctx_p->user.key. But
> ctx_p->user.key is still used in the next line, which will lead to a
> use after free.
> 
> We can call kfree_sensitive() after dev_dbg() to avoid the uaf.
> 
> Signed-off-by: Jianglei Nie <niejianglei2021@163.com>
> ---
>  drivers/crypto/ccree/cc_cipher.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