linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/4] crypto: async crypto op fixes
@ 2017-05-11 11:53 Gilad Ben-Yossef
  2017-05-11 11:53 ` [PATCH 1/4] crypto: handle EBUSY due to backlog correctly Gilad Ben-Yossef
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Gilad Ben-Yossef @ 2017-05-11 11:53 UTC (permalink / raw)
  To: Herbert Xu, David S. Miller, Jonathan Corbet, David Howells
  Cc: Ofir Drang, Gilad Ben-Yossef, Eric Biggers, stable, linux-crypto,
	linux-doc, linux-kernel, keyrings

This patch set fixes various usage and documentation errors
in waiting for async crypto op to complete which can result
in data corruption.

Note: these were discovered in the process of working on a
patch set that replaces these call sites and more with a
generic implementation that will prevent these problems
going forward. These are just the fix ups for current code.

Signed-off-by: Gilad Ben-Yossef <gilad@benyossef.com>
CC: stable@vger.kernel.org
CC: Eric Biggers <ebiggers3@gmail.com>

Gilad Ben-Yossef (4):
  crypto: handle EBUSY due to backlog correctly
  crypto: drbg wait for crypto op not signal safe
  crypto: gcm wait for crypto op not signal safe
  crypto: Documentation: fix none signal safe sample

 Documentation/crypto/api-samples.rst | 2 +-
 crypto/asymmetric_keys/public_key.c  | 2 +-
 crypto/drbg.c                        | 5 ++---
 crypto/gcm.c                         | 6 ++----
 4 files changed, 6 insertions(+), 9 deletions(-)

-- 
2.1.4

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

* [PATCH 1/4] crypto: handle EBUSY due to backlog correctly
  2017-05-11 11:53 [PATCH 0/4] crypto: async crypto op fixes Gilad Ben-Yossef
@ 2017-05-11 11:53 ` Gilad Ben-Yossef
  2017-05-11 11:53 ` [PATCH 2/4] crypto: drbg wait for crypto op not signal safe Gilad Ben-Yossef
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Gilad Ben-Yossef @ 2017-05-11 11:53 UTC (permalink / raw)
  To: Herbert Xu, David S. Miller, Jonathan Corbet, David Howells
  Cc: Ofir Drang, Gilad Ben-Yossef, Eric Biggers, stable, linux-crypto,
	linux-doc, linux-kernel, keyrings

public_key_verify_signature() was passing the CRYPTO_TFM_REQ_MAY_BACKLOG
flag to akcipher_request_set_callback() but was not handling correctly
the case where a -EBUSY error could be returned from the call to
crypto_akcipher_verify() if backlog was used, possibly casuing
data corruption due to use-after-free of buffers.

Resolve this by handling -EBUSY correctly.

Signed-off-by: Gilad Ben-Yossef <gilad@benyossef.com>
CC: stable@vger.kernel.org
---
 crypto/asymmetric_keys/public_key.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/crypto/asymmetric_keys/public_key.c b/crypto/asymmetric_keys/public_key.c
index d3a989e..3cd6e12 100644
--- a/crypto/asymmetric_keys/public_key.c
+++ b/crypto/asymmetric_keys/public_key.c
@@ -141,7 +141,7 @@ int public_key_verify_signature(const struct public_key *pkey,
 	 * signature and returns that to us.
 	 */
 	ret = crypto_akcipher_verify(req);
-	if (ret == -EINPROGRESS) {
+	if ((ret == -EINPROGRESS) || (ret == -EBUSY)) {
 		wait_for_completion(&compl.completion);
 		ret = compl.err;
 	}
-- 
2.1.4

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

* [PATCH 2/4] crypto: drbg wait for crypto op not signal safe
  2017-05-11 11:53 [PATCH 0/4] crypto: async crypto op fixes Gilad Ben-Yossef
  2017-05-11 11:53 ` [PATCH 1/4] crypto: handle EBUSY due to backlog correctly Gilad Ben-Yossef
@ 2017-05-11 11:53 ` Gilad Ben-Yossef
  2017-05-16 22:39   ` Eric Biggers
  2017-05-18  5:09   ` Herbert Xu
  2017-05-11 11:53 ` [PATCH 3/4] crypto: gcm " Gilad Ben-Yossef
  2017-05-11 11:53 ` [PATCH 4/4] crypto: Documentation: fix none signal safe sample Gilad Ben-Yossef
  3 siblings, 2 replies; 9+ messages in thread
From: Gilad Ben-Yossef @ 2017-05-11 11:53 UTC (permalink / raw)
  To: Herbert Xu, David S. Miller, Jonathan Corbet, David Howells
  Cc: Ofir Drang, Gilad Ben-Yossef, Eric Biggers, stable, linux-crypto,
	linux-doc, linux-kernel, keyrings

drbg_kcapi_sym_ctr() was using wait_for_completion_interruptible() to
wait for completion of async crypto op but if a signal occurs it
may return before DMA ops of HW crypto provider finish, thus
corrupting the output buffer.

Resolve this by using wait_for_completion() instead.

Reported-by: Eric Biggers <ebiggers3@gmail.com>
Signed-off-by: Gilad Ben-Yossef <gilad@benyossef.com>
CC: stable@vger.kernel.org
---
 crypto/drbg.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/crypto/drbg.c b/crypto/drbg.c
index fa749f4..fa9054d 100644
--- a/crypto/drbg.c
+++ b/crypto/drbg.c
@@ -1767,8 +1767,7 @@ static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
 			break;
 		case -EINPROGRESS:
 		case -EBUSY:
-			ret = wait_for_completion_interruptible(
-				&drbg->ctr_completion);
+			ret = wait_for_completion(&drbg->ctr_completion);
 			if (!ret && !drbg->ctr_async_err) {
 				reinit_completion(&drbg->ctr_completion);
 				break;
-- 
2.1.4

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

* [PATCH 3/4] crypto: gcm wait for crypto op not signal safe
  2017-05-11 11:53 [PATCH 0/4] crypto: async crypto op fixes Gilad Ben-Yossef
  2017-05-11 11:53 ` [PATCH 1/4] crypto: handle EBUSY due to backlog correctly Gilad Ben-Yossef
  2017-05-11 11:53 ` [PATCH 2/4] crypto: drbg wait for crypto op not signal safe Gilad Ben-Yossef
@ 2017-05-11 11:53 ` Gilad Ben-Yossef
  2017-05-11 11:53 ` [PATCH 4/4] crypto: Documentation: fix none signal safe sample Gilad Ben-Yossef
  3 siblings, 0 replies; 9+ messages in thread
From: Gilad Ben-Yossef @ 2017-05-11 11:53 UTC (permalink / raw)
  To: Herbert Xu, David S. Miller, Jonathan Corbet, David Howells
  Cc: Ofir Drang, Gilad Ben-Yossef, Eric Biggers, stable, linux-crypto,
	linux-doc, linux-kernel, keyrings

crypto_gcm_setkey() was using wait_for_completion_interruptible() to
wait for completion of async crypto op but if a signal occurs it
may return before DMA ops of HW crypto provider finish, thus
corrupting the data buffer that is kfree'ed in this case.

Resolve this by using wait_for_completion() instead.

Reported-by: Eric Biggers <ebiggers3@gmail.com>
Signed-off-by: Gilad Ben-Yossef <gilad@benyossef.com>
CC: stable@vger.kernel.org
---
 crypto/drbg.c | 4 ++--
 crypto/gcm.c  | 6 ++----
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/crypto/drbg.c b/crypto/drbg.c
index fa9054d..cdb27ac 100644
--- a/crypto/drbg.c
+++ b/crypto/drbg.c
@@ -1767,8 +1767,8 @@ static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
 			break;
 		case -EINPROGRESS:
 		case -EBUSY:
-			ret = wait_for_completion(&drbg->ctr_completion);
-			if (!ret && !drbg->ctr_async_err) {
+			wait_for_completion(&drbg->ctr_completion);
+			if (!drbg->ctr_async_err) {
 				reinit_completion(&drbg->ctr_completion);
 				break;
 			}
diff --git a/crypto/gcm.c b/crypto/gcm.c
index b7ad808..3841b5e 100644
--- a/crypto/gcm.c
+++ b/crypto/gcm.c
@@ -152,10 +152,8 @@ static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
 
 	err = crypto_skcipher_encrypt(&data->req);
 	if (err == -EINPROGRESS || err == -EBUSY) {
-		err = wait_for_completion_interruptible(
-			&data->result.completion);
-		if (!err)
-			err = data->result.err;
+		wait_for_completion(&data->result.completion);
+		err = data->result.err;
 	}
 
 	if (err)
-- 
2.1.4

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

* [PATCH 4/4] crypto: Documentation: fix none signal safe sample
  2017-05-11 11:53 [PATCH 0/4] crypto: async crypto op fixes Gilad Ben-Yossef
                   ` (2 preceding siblings ...)
  2017-05-11 11:53 ` [PATCH 3/4] crypto: gcm " Gilad Ben-Yossef
@ 2017-05-11 11:53 ` Gilad Ben-Yossef
  2017-05-16 22:41   ` Eric Biggers
  3 siblings, 1 reply; 9+ messages in thread
From: Gilad Ben-Yossef @ 2017-05-11 11:53 UTC (permalink / raw)
  To: Herbert Xu, David S. Miller, Jonathan Corbet, David Howells
  Cc: Ofir Drang, Gilad Ben-Yossef, Eric Biggers, linux-crypto,
	linux-doc, linux-kernel, keyrings

The sample code was showing use of wait_for_completion_interruptible()
for waiting for an async. crypto op to finish. However, if a signal
arrived it would free the buffers used even while crypto HW might
still DMA from/into the buffers.

Resolve this by using wait_for_completion() instead.

Reported-by: Eric Biggers <ebiggers3@gmail.com>
Signed-off-by: Gilad Ben-Yossef <gilad@benyossef.com>
---
 Documentation/crypto/api-samples.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Documentation/crypto/api-samples.rst b/Documentation/crypto/api-samples.rst
index d021fd9..944f08b 100644
--- a/Documentation/crypto/api-samples.rst
+++ b/Documentation/crypto/api-samples.rst
@@ -48,7 +48,7 @@ Code Example For Symmetric Key Cipher Operation
             break;
         case -EINPROGRESS:
         case -EBUSY:
-            rc = wait_for_completion_interruptible(
+            rc = wait_for_completion(
                 &sk->result.completion);
             if (!rc && !sk->result.err) {
                 reinit_completion(&sk->result.completion);
-- 
2.1.4

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

* Re: [PATCH 2/4] crypto: drbg wait for crypto op not signal safe
  2017-05-11 11:53 ` [PATCH 2/4] crypto: drbg wait for crypto op not signal safe Gilad Ben-Yossef
@ 2017-05-16 22:39   ` Eric Biggers
  2017-05-18  5:09   ` Herbert Xu
  1 sibling, 0 replies; 9+ messages in thread
From: Eric Biggers @ 2017-05-16 22:39 UTC (permalink / raw)
  To: Gilad Ben-Yossef
  Cc: Herbert Xu, David S. Miller, Jonathan Corbet, David Howells,
	Ofir Drang, Gilad Ben-Yossef, stable, linux-crypto, linux-doc,
	linux-kernel, keyrings

Hi Gilad,

On Thu, May 11, 2017 at 02:53:43PM +0300, Gilad Ben-Yossef wrote:
> drbg_kcapi_sym_ctr() was using wait_for_completion_interruptible() to
> wait for completion of async crypto op but if a signal occurs it
> may return before DMA ops of HW crypto provider finish, thus
> corrupting the output buffer.
> 
> Resolve this by using wait_for_completion() instead.
> 
> Reported-by: Eric Biggers <ebiggers3@gmail.com>
> Signed-off-by: Gilad Ben-Yossef <gilad@benyossef.com>
> CC: stable@vger.kernel.org
> ---
>  crypto/drbg.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/crypto/drbg.c b/crypto/drbg.c
> index fa749f4..fa9054d 100644
> --- a/crypto/drbg.c
> +++ b/crypto/drbg.c
> @@ -1767,8 +1767,7 @@ static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
>  			break;
>  		case -EINPROGRESS:
>  		case -EBUSY:
> -			ret = wait_for_completion_interruptible(
> -				&drbg->ctr_completion);
> +			ret = wait_for_completion(&drbg->ctr_completion);
>  			if (!ret && !drbg->ctr_async_err) {
>  				reinit_completion(&drbg->ctr_completion);
>  				break;
> -- 

wait_for_completion() doesn't return a value.  This was fixed in the next patch,
but it should be done in this patch.

Eric

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

* Re: [PATCH 4/4] crypto: Documentation: fix none signal safe sample
  2017-05-11 11:53 ` [PATCH 4/4] crypto: Documentation: fix none signal safe sample Gilad Ben-Yossef
@ 2017-05-16 22:41   ` Eric Biggers
  0 siblings, 0 replies; 9+ messages in thread
From: Eric Biggers @ 2017-05-16 22:41 UTC (permalink / raw)
  To: Gilad Ben-Yossef
  Cc: Herbert Xu, David S. Miller, Jonathan Corbet, David Howells,
	Ofir Drang, Gilad Ben-Yossef, linux-crypto, linux-doc,
	linux-kernel, keyrings

On Thu, May 11, 2017 at 02:53:45PM +0300, Gilad Ben-Yossef wrote:
> The sample code was showing use of wait_for_completion_interruptible()
> for waiting for an async. crypto op to finish. However, if a signal
> arrived it would free the buffers used even while crypto HW might
> still DMA from/into the buffers.
> 
> Resolve this by using wait_for_completion() instead.
> 
> Reported-by: Eric Biggers <ebiggers3@gmail.com>
> Signed-off-by: Gilad Ben-Yossef <gilad@benyossef.com>
> ---
>  Documentation/crypto/api-samples.rst | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/Documentation/crypto/api-samples.rst b/Documentation/crypto/api-samples.rst
> index d021fd9..944f08b 100644
> --- a/Documentation/crypto/api-samples.rst
> +++ b/Documentation/crypto/api-samples.rst
> @@ -48,7 +48,7 @@ Code Example For Symmetric Key Cipher Operation
>              break;
>          case -EINPROGRESS:
>          case -EBUSY:
> -            rc = wait_for_completion_interruptible(
> +            rc = wait_for_completion(
>                  &sk->result.completion);
>              if (!rc && !sk->result.err) {
>                  reinit_completion(&sk->result.completion);
> -- 
> 2.1.4
> 

Same issue here: wait_for_completion() doesn't return a value.

Eric

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

* Re: [PATCH 2/4] crypto: drbg wait for crypto op not signal safe
  2017-05-11 11:53 ` [PATCH 2/4] crypto: drbg wait for crypto op not signal safe Gilad Ben-Yossef
  2017-05-16 22:39   ` Eric Biggers
@ 2017-05-18  5:09   ` Herbert Xu
  2017-05-18  9:23     ` Gilad Ben-Yossef
  1 sibling, 1 reply; 9+ messages in thread
From: Herbert Xu @ 2017-05-18  5:09 UTC (permalink / raw)
  To: Gilad Ben-Yossef
  Cc: David S. Miller, Jonathan Corbet, David Howells, Ofir Drang,
	Gilad Ben-Yossef, Eric Biggers, stable, linux-crypto, linux-doc,
	linux-kernel, keyrings

On Thu, May 11, 2017 at 02:53:43PM +0300, Gilad Ben-Yossef wrote:
> drbg_kcapi_sym_ctr() was using wait_for_completion_interruptible() to
> wait for completion of async crypto op but if a signal occurs it
> may return before DMA ops of HW crypto provider finish, thus
> corrupting the output buffer.
> 
> Resolve this by using wait_for_completion() instead.
> 
> Reported-by: Eric Biggers <ebiggers3@gmail.com>
> Signed-off-by: Gilad Ben-Yossef <gilad@benyossef.com>
> CC: stable@vger.kernel.org

This patch doesn't even compile.  Please test your work first.

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

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

* Re: [PATCH 2/4] crypto: drbg wait for crypto op not signal safe
  2017-05-18  5:09   ` Herbert Xu
@ 2017-05-18  9:23     ` Gilad Ben-Yossef
  0 siblings, 0 replies; 9+ messages in thread
From: Gilad Ben-Yossef @ 2017-05-18  9:23 UTC (permalink / raw)
  To: Herbert Xu
  Cc: David S. Miller, Jonathan Corbet, David Howells, Ofir Drang,
	Gilad Ben-Yossef, Eric Biggers, stable, linux-crypto, linux-doc,
	Linux kernel mailing list, keyrings

On Thu, May 18, 2017 at 8:09 AM, Herbert Xu <herbert@gondor.apana.org.au> wrote:
> On Thu, May 11, 2017 at 02:53:43PM +0300, Gilad Ben-Yossef wrote:
>> drbg_kcapi_sym_ctr() was using wait_for_completion_interruptible() to
>> wait for completion of async crypto op but if a signal occurs it
>> may return before DMA ops of HW crypto provider finish, thus
>> corrupting the output buffer.
>>
>> Resolve this by using wait_for_completion() instead.
>>
>> Reported-by: Eric Biggers <ebiggers3@gmail.com>
>> Signed-off-by: Gilad Ben-Yossef <gilad@benyossef.com>
>> CC: stable@vger.kernel.org
>
> This patch doesn't even compile.  Please test your work first.

Sigh... I've noticed it, fixed it, compiled it and than went ahead and
squashed the fix with the next patch in series instead of this one
like an idiot.

Please accept my apologies for wasting your time. I'll send a fixed version.

Gilad

>
> 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



-- 
Gilad Ben-Yossef
Chief Coffee Drinker

"If you take a class in large-scale robotics, can you end up in a
situation where the homework eats your dog?"
 -- Jean-Baptiste Queru

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

end of thread, other threads:[~2017-05-18  9:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-11 11:53 [PATCH 0/4] crypto: async crypto op fixes Gilad Ben-Yossef
2017-05-11 11:53 ` [PATCH 1/4] crypto: handle EBUSY due to backlog correctly Gilad Ben-Yossef
2017-05-11 11:53 ` [PATCH 2/4] crypto: drbg wait for crypto op not signal safe Gilad Ben-Yossef
2017-05-16 22:39   ` Eric Biggers
2017-05-18  5:09   ` Herbert Xu
2017-05-18  9:23     ` Gilad Ben-Yossef
2017-05-11 11:53 ` [PATCH 3/4] crypto: gcm " Gilad Ben-Yossef
2017-05-11 11:53 ` [PATCH 4/4] crypto: Documentation: fix none signal safe sample Gilad Ben-Yossef
2017-05-16 22:41   ` Eric Biggers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).