All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/4] crypto: caam - fix use-after-free KASAN issue
@ 2020-04-05 11:50 Iuliana Prodan
  2020-04-05 11:50 ` [PATCH 1/4] crypto: caam - fix use-after-free KASAN issue for SKCIPHER algorithms Iuliana Prodan
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Iuliana Prodan @ 2020-04-05 11:50 UTC (permalink / raw)
  To: Herbert Xu, Horia Geanta, Aymen Sghaier
  Cc: David S. Miller, Silvano Di Ninno, Franck Lenormand,
	linux-crypto, linux-kernel, linux-imx, Iuliana Prodan

This series fixes a use-after-free KASAN issue that was introduce by CAAM backlogging support[1].
This is fixed for SKCIPHER, HASH, RSA and AEAD algorithms.

[1] https://patchwork.kernel.org/cover/11378941/

Iuliana Prodan (4):
  crypto: caam - fix use-after-free KASAN issue for SKCIPHER algorithms
  crypto: caam - fix use-after-free KASAN issue for AEAD algorithms
  crypto: caam - fix use-after-free KASAN issue for HASH algorithms
  crypto: caam - fix use-after-free KASAN issue for RSA algorithms

 drivers/crypto/caam/caamalg.c  | 8 ++++----
 drivers/crypto/caam/caamhash.c | 5 +++--
 drivers/crypto/caam/caampkc.c  | 6 ++++--
 3 files changed, 11 insertions(+), 8 deletions(-)

-- 
2.1.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/4] crypto: caam - fix use-after-free KASAN issue for SKCIPHER algorithms
  2020-04-05 11:50 [PATCH 0/4] crypto: caam - fix use-after-free KASAN issue Iuliana Prodan
@ 2020-04-05 11:50 ` Iuliana Prodan
  2020-04-05 11:50 ` [PATCH 2/4] crypto: caam - fix use-after-free KASAN issue for AEAD algorithms Iuliana Prodan
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Iuliana Prodan @ 2020-04-05 11:50 UTC (permalink / raw)
  To: Herbert Xu, Horia Geanta, Aymen Sghaier
  Cc: David S. Miller, Silvano Di Ninno, Franck Lenormand,
	linux-crypto, linux-kernel, linux-imx, Iuliana Prodan

Here's the KASAN report:
BUG: KASAN: use-after-free in skcipher_crypt_done+0xe8/0x1a8
Read of size 1 at addr ffff00002304001c by task swapper/0/0

CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.6.0-rc1-00162-gfcb90d5 #9
Hardware name: LS1046A RDB Board (DT)
Call trace:
 dump_backtrace+0x0/0x260
 show_stack+0x14/0x20
 dump_stack+0xe8/0x144
 print_address_description.isra.11+0x64/0x348
 __kasan_report+0x11c/0x230
 kasan_report+0xc/0x18
 __asan_load1+0x5c/0x68
 skcipher_crypt_done+0xe8/0x1a8
 caam_jr_dequeue+0x390/0x608
 tasklet_action_common.isra.13+0x1ec/0x230
 ...

Fixes: ee38767f152a ("crypto: caam - support crypto_engine framework for SKCIPHER algorithms")
Signed-off-by: Iuliana Prodan <iuliana.prodan@nxp.com>
---
 drivers/crypto/caam/caamalg.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c
index b7bb7c3..5538539 100644
--- a/drivers/crypto/caam/caamalg.c
+++ b/drivers/crypto/caam/caamalg.c
@@ -1022,8 +1022,6 @@ static void skcipher_crypt_done(struct device *jrdev, u32 *desc, u32 err,
 		     DUMP_PREFIX_ADDRESS, 16, 4, req->dst,
 		     edesc->dst_nents > 1 ? 100 : req->cryptlen, 1);
 
-	kfree(edesc);
-
 	/*
 	 * If no backlog flag, the completion of the request is done
 	 * by CAAM, not crypto engine.
@@ -1032,6 +1030,8 @@ static void skcipher_crypt_done(struct device *jrdev, u32 *desc, u32 err,
 		skcipher_request_complete(req, ecode);
 	else
 		crypto_finalize_skcipher_request(jrp->engine, req, ecode);
+
+	kfree(edesc);
 }
 
 /*
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/4] crypto: caam - fix use-after-free KASAN issue for AEAD algorithms
  2020-04-05 11:50 [PATCH 0/4] crypto: caam - fix use-after-free KASAN issue Iuliana Prodan
  2020-04-05 11:50 ` [PATCH 1/4] crypto: caam - fix use-after-free KASAN issue for SKCIPHER algorithms Iuliana Prodan
@ 2020-04-05 11:50 ` Iuliana Prodan
  2020-04-06 11:04   ` Horia Geantă
  2020-04-05 11:50 ` [PATCH 3/4] crypto: caam - fix use-after-free KASAN issue for HASH algorithms Iuliana Prodan
  2020-04-05 11:50 ` [PATCH 4/4] crypto: caam - fix use-after-free KASAN issue for RSA algorithms Iuliana Prodan
  3 siblings, 1 reply; 6+ messages in thread
From: Iuliana Prodan @ 2020-04-05 11:50 UTC (permalink / raw)
  To: Herbert Xu, Horia Geanta, Aymen Sghaier
  Cc: David S. Miller, Silvano Di Ninno, Franck Lenormand,
	linux-crypto, linux-kernel, linux-imx, Iuliana Prodan

Here's the KASAN report:
BUG: KASAN: use-after-free in aead_crypt_done+0x60/0xd8
Read of size 1 at addr ffff00002303f014 by task swapper/0/0

CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.6.0-rc1-00163-gd88dd5c-dirty #18
Hardware name: LS1046A RDB Board (DT)
Call trace:
 dump_backtrace+0x0/0x260
 show_stack+0x14/0x20
 dump_stack+0xe8/0x144
 print_address_description.isra.11+0x64/0x348
 __kasan_report+0x11c/0x230
 kasan_report+0xc/0x18
 __asan_load1+0x5c/0x68
 aead_crypt_done+0x60/0xd8
 caam_jr_dequeue+0x390/0x608
...

Fixes: 1c2402266713 ("crypto: caam - add crypto_engine support for AEAD algorithms")
Signed-off-by: Iuliana Prodan <iuliana.prodan@nxp.com>
---
 drivers/crypto/caam/caamalg.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/crypto/caam/caamalg.c b/drivers/crypto/caam/caamalg.c
index 5538539..6d746ef 100644
--- a/drivers/crypto/caam/caamalg.c
+++ b/drivers/crypto/caam/caamalg.c
@@ -973,8 +973,6 @@ static void aead_crypt_done(struct device *jrdev, u32 *desc, u32 err,
 
 	aead_unmap(jrdev, edesc, req);
 
-	kfree(edesc);
-
 	/*
 	 * If no backlog flag, the completion of the request is done
 	 * by CAAM, not crypto engine.
@@ -983,6 +981,8 @@ static void aead_crypt_done(struct device *jrdev, u32 *desc, u32 err,
 		aead_request_complete(req, ecode);
 	else
 		crypto_finalize_aead_request(jrp->engine, req, ecode);
+
+	kfree(edesc);
 }
 
 static void skcipher_crypt_done(struct device *jrdev, u32 *desc, u32 err,
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/4] crypto: caam - fix use-after-free KASAN issue for HASH algorithms
  2020-04-05 11:50 [PATCH 0/4] crypto: caam - fix use-after-free KASAN issue Iuliana Prodan
  2020-04-05 11:50 ` [PATCH 1/4] crypto: caam - fix use-after-free KASAN issue for SKCIPHER algorithms Iuliana Prodan
  2020-04-05 11:50 ` [PATCH 2/4] crypto: caam - fix use-after-free KASAN issue for AEAD algorithms Iuliana Prodan
@ 2020-04-05 11:50 ` Iuliana Prodan
  2020-04-05 11:50 ` [PATCH 4/4] crypto: caam - fix use-after-free KASAN issue for RSA algorithms Iuliana Prodan
  3 siblings, 0 replies; 6+ messages in thread
From: Iuliana Prodan @ 2020-04-05 11:50 UTC (permalink / raw)
  To: Herbert Xu, Horia Geanta, Aymen Sghaier
  Cc: David S. Miller, Silvano Di Ninno, Franck Lenormand,
	linux-crypto, linux-kernel, linux-imx, Iuliana Prodan

Here's the KASAN report:
BUG: KASAN: use-after-free in ahash_done+0xdc/0x3b8
Read of size 1 at addr ffff000022e75510 by task swapper/0/0

CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.6.0-rc1-00164-gcb59d59-dirty #20
Hardware name: LS1046A RDB Board (DT)
Call trace:
 dump_backtrace+0x0/0x260
 show_stack+0x14/0x20
 dump_stack+0xe8/0x144
 print_address_description.isra.11+0x64/0x348
 __kasan_report+0x11c/0x230
 kasan_report+0xc/0x18
 __asan_load1+0x5c/0x68
 ahash_done+0xdc/0x3b8
 caam_jr_dequeue+0x390/0x608
 tasklet_action_common.isra.13+0x1ec/0x230

Fixes: 21b014f038d3 ("crypto: caam - add crypto_engine support for HASH algorithms")
Signed-off-by: Iuliana Prodan <iuliana.prodan@nxp.com>
---
 drivers/crypto/caam/caamhash.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/crypto/caam/caamhash.c b/drivers/crypto/caam/caamhash.c
index 943bc02..3c63fe0 100644
--- a/drivers/crypto/caam/caamhash.c
+++ b/drivers/crypto/caam/caamhash.c
@@ -593,7 +593,6 @@ static inline void ahash_done_cpy(struct device *jrdev, u32 *desc, u32 err,
 
 	ahash_unmap_ctx(jrdev, edesc, req, digestsize, dir);
 	memcpy(req->result, state->caam_ctx, digestsize);
-	kfree(edesc);
 
 	print_hex_dump_debug("ctx@"__stringify(__LINE__)": ",
 			     DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
@@ -607,6 +606,8 @@ static inline void ahash_done_cpy(struct device *jrdev, u32 *desc, u32 err,
 		req->base.complete(&req->base, ecode);
 	else
 		crypto_finalize_hash_request(jrp->engine, req, ecode);
+
+	kfree(edesc);
 }
 
 static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
@@ -640,7 +641,6 @@ static inline void ahash_done_switch(struct device *jrdev, u32 *desc, u32 err,
 		ecode = caam_jr_strstatus(jrdev, err);
 
 	ahash_unmap_ctx(jrdev, edesc, req, ctx->ctx_len, dir);
-	kfree(edesc);
 
 	scatterwalk_map_and_copy(state->buf, req->src,
 				 req->nbytes - state->next_buflen,
@@ -668,6 +668,7 @@ static inline void ahash_done_switch(struct device *jrdev, u32 *desc, u32 err,
 	else
 		crypto_finalize_hash_request(jrp->engine, req, ecode);
 
+	kfree(edesc);
 }
 
 static void ahash_done_bi(struct device *jrdev, u32 *desc, u32 err,
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 4/4] crypto: caam - fix use-after-free KASAN issue for RSA algorithms
  2020-04-05 11:50 [PATCH 0/4] crypto: caam - fix use-after-free KASAN issue Iuliana Prodan
                   ` (2 preceding siblings ...)
  2020-04-05 11:50 ` [PATCH 3/4] crypto: caam - fix use-after-free KASAN issue for HASH algorithms Iuliana Prodan
@ 2020-04-05 11:50 ` Iuliana Prodan
  3 siblings, 0 replies; 6+ messages in thread
From: Iuliana Prodan @ 2020-04-05 11:50 UTC (permalink / raw)
  To: Herbert Xu, Horia Geanta, Aymen Sghaier
  Cc: David S. Miller, Silvano Di Ninno, Franck Lenormand,
	linux-crypto, linux-kernel, linux-imx, Iuliana Prodan

Here's the KASAN report:
BUG: KASAN: use-after-free in rsa_pub_done+0x70/0xe8
Read of size 1 at addr ffff00002267b614 by task swapper/0/0

CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.6.0-rc1-00165-ga85dda9-dirty #21
Hardware name: LS1046A RDB Board (DT)
Call trace:
 dump_backtrace+0x0/0x260
 show_stack+0x14/0x20
 dump_stack+0xe8/0x144
 print_address_description.isra.11+0x64/0x348
 __kasan_report+0x11c/0x230
 kasan_report+0xc/0x18
 __asan_load1+0x5c/0x68
 rsa_pub_done+0x70/0xe8
 caam_jr_dequeue+0x390/0x608
 tasklet_action_common.isra.13+0x1ec/0x230
...

Fixes: bf53795025a2 ("crypto: caam - add crypto_engine support for RSA algorithms")
Signed-off-by: Iuliana Prodan <iuliana.prodan@nxp.com>
---
 drivers/crypto/caam/caampkc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/crypto/caam/caampkc.c b/drivers/crypto/caam/caampkc.c
index 4fcae37..59cc7116 100644
--- a/drivers/crypto/caam/caampkc.c
+++ b/drivers/crypto/caam/caampkc.c
@@ -129,7 +129,6 @@ static void rsa_pub_done(struct device *dev, u32 *desc, u32 err, void *context)
 
 	rsa_pub_unmap(dev, edesc, req);
 	rsa_io_unmap(dev, edesc, req);
-	kfree(edesc);
 
 	/*
 	 * If no backlog flag, the completion of the request is done
@@ -139,6 +138,8 @@ static void rsa_pub_done(struct device *dev, u32 *desc, u32 err, void *context)
 		akcipher_request_complete(req, ecode);
 	else
 		crypto_finalize_akcipher_request(jrp->engine, req, ecode);
+
+	kfree(edesc);
 }
 
 static void rsa_priv_f_done(struct device *dev, u32 *desc, u32 err,
@@ -170,7 +171,6 @@ static void rsa_priv_f_done(struct device *dev, u32 *desc, u32 err,
 	}
 
 	rsa_io_unmap(dev, edesc, req);
-	kfree(edesc);
 
 	/*
 	 * If no backlog flag, the completion of the request is done
@@ -180,6 +180,8 @@ static void rsa_priv_f_done(struct device *dev, u32 *desc, u32 err,
 		akcipher_request_complete(req, ecode);
 	else
 		crypto_finalize_akcipher_request(jrp->engine, req, ecode);
+
+	kfree(edesc);
 }
 
 /**
-- 
2.1.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/4] crypto: caam - fix use-after-free KASAN issue for AEAD algorithms
  2020-04-05 11:50 ` [PATCH 2/4] crypto: caam - fix use-after-free KASAN issue for AEAD algorithms Iuliana Prodan
@ 2020-04-06 11:04   ` Horia Geantă
  0 siblings, 0 replies; 6+ messages in thread
From: Horia Geantă @ 2020-04-06 11:04 UTC (permalink / raw)
  To: Iuliana Prodan, Herbert Xu, Aymen Sghaier
  Cc: David S. Miller, Silvano Di Ninno, Franck Lenormand,
	linux-crypto, linux-kernel, dl-linux-imx

On 4/5/2020 2:50 PM, Iuliana Prodan wrote:
> Here's the KASAN report:
> BUG: KASAN: use-after-free in aead_crypt_done+0x60/0xd8
> Read of size 1 at addr ffff00002303f014 by task swapper/0/0
> 
> CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.6.0-rc1-00163-gd88dd5c-dirty #18
This is no a public SHA1, you are probably running with patch 1/4
applied in the tree (and with a "dirty tree").

> Hardware name: LS1046A RDB Board (DT)
> Call trace:
>  dump_backtrace+0x0/0x260
>  show_stack+0x14/0x20
>  dump_stack+0xe8/0x144
>  print_address_description.isra.11+0x64/0x348
>  __kasan_report+0x11c/0x230
>  kasan_report+0xc/0x18
>  __asan_load1+0x5c/0x68
>  aead_crypt_done+0x60/0xd8
>  caam_jr_dequeue+0x390/0x608
> ...
You should provide full KASan log - shawdow bits etc.

> @@ -973,8 +973,6 @@ static void aead_crypt_done(struct device *jrdev, u32 *desc, u32 err,
>  
>  	aead_unmap(jrdev, edesc, req);
>  
> -	kfree(edesc);
> -
>  	/*
>  	 * If no backlog flag, the completion of the request is done
>  	 * by CAAM, not crypto engine.
> @@ -983,6 +981,8 @@ static void aead_crypt_done(struct device *jrdev, u32 *desc, u32 err,
>  		aead_request_complete(req, ecode);
>  	else
>  		crypto_finalize_aead_request(jrp->engine, req, ecode);
> +
> +	kfree(edesc);
I think it's better freeing all resources before calling
the completion callback, to avoid unnecessary memory strains.

Horia

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-04-06 11:04 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-05 11:50 [PATCH 0/4] crypto: caam - fix use-after-free KASAN issue Iuliana Prodan
2020-04-05 11:50 ` [PATCH 1/4] crypto: caam - fix use-after-free KASAN issue for SKCIPHER algorithms Iuliana Prodan
2020-04-05 11:50 ` [PATCH 2/4] crypto: caam - fix use-after-free KASAN issue for AEAD algorithms Iuliana Prodan
2020-04-06 11:04   ` Horia Geantă
2020-04-05 11:50 ` [PATCH 3/4] crypto: caam - fix use-after-free KASAN issue for HASH algorithms Iuliana Prodan
2020-04-05 11:50 ` [PATCH 4/4] crypto: caam - fix use-after-free KASAN issue for RSA algorithms Iuliana Prodan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.