diff mbox series

[v4,2/7] crypto: aead - disallow en/decrypt for non-task or non-softirq context

Message ID 20210519112239.33664-3-ardb@kernel.org
State New
Headers show
Series running kernel mode SIMD with softirqs disabled | expand

Commit Message

Ard Biesheuvel May 19, 2021, 11:22 a.m. UTC
In order to ensure that kernel mode SIMD routines will not need a scalar
fallback if they run with softirqs disabled, disallow any use of the
AEAD encrypt and decrypt routines from outside of task or softirq context.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 crypto/aead.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

Comments

Herbert Xu May 19, 2021, 11:29 a.m. UTC | #1
On Wed, May 19, 2021 at 01:22:34PM +0200, Ard Biesheuvel wrote:
>
>  	crypto_stats_get(alg);
> -	if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
> +	if (!(alg->cra_flags & CRYPTO_ALG_ASYNC) &&
> +	    WARN_ONCE(!in_task() && !in_serving_softirq(),
> +		      "synchronous call from invalid context\n"))
> +		ret = -EBUSY;

I don't think we've ever supported crypto in hard IRQ contexts.
So this should be done regardless of ASYNC.

Then again, do we really need this since the assumption has
always been that the crypto API can only be invoked in user or
softirq context?

Cheers,
Herbert Xu May 19, 2021, 11:51 a.m. UTC | #2
On Wed, May 19, 2021 at 01:36:37PM +0200, Ard Biesheuvel wrote:
> 
> So if we do need to check this, we should check it here. If we don't,
> then we can drop these patches.

Historically other things would break in nasty ways if you tried
to do crypto in hard IRQ contexts, e.g., overwritten kmap slots
back when we had individual slots for each context, but I don't
think we've ever found anyone crazy enough to do that to warrant
a run-time check.

I'd just leave it out for now.

Thanks,
Ard Biesheuvel May 21, 2021, 7:30 a.m. UTC | #3
On Wed, 19 May 2021 at 13:51, Herbert Xu <herbert@gondor.apana.org.au> wrote:
>

> On Wed, May 19, 2021 at 01:36:37PM +0200, Ard Biesheuvel wrote:

> >

> > So if we do need to check this, we should check it here. If we don't,

> > then we can drop these patches.

>

> Historically other things would break in nasty ways if you tried

> to do crypto in hard IRQ contexts, e.g., overwritten kmap slots

> back when we had individual slots for each context, but I don't

> think we've ever found anyone crazy enough to do that to warrant

> a run-time check.

>

> I'd just leave it out for now.

>


Fair enough. Would you like me to resend the series with these patches
left out Or are you ok to just take the remaining ones (assuming there
are no issues reported with those)?
diff mbox series

Patch

diff --git a/crypto/aead.c b/crypto/aead.c
index 16991095270d..141c9369b02a 100644
--- a/crypto/aead.c
+++ b/crypto/aead.c
@@ -88,7 +88,11 @@  int crypto_aead_encrypt(struct aead_request *req)
 	int ret;
 
 	crypto_stats_get(alg);
-	if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
+	if (!(alg->cra_flags & CRYPTO_ALG_ASYNC) &&
+	    WARN_ONCE(!in_task() && !in_serving_softirq(),
+		      "synchronous call from invalid context\n"))
+		ret = -EBUSY;
+	else if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
 		ret = -ENOKEY;
 	else
 		ret = crypto_aead_alg(aead)->encrypt(req);
@@ -105,7 +109,11 @@  int crypto_aead_decrypt(struct aead_request *req)
 	int ret;
 
 	crypto_stats_get(alg);
-	if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
+	if (!(alg->cra_flags & CRYPTO_ALG_ASYNC) &&
+	    WARN_ONCE(!in_task() && !in_serving_softirq(),
+		      "synchronous call from invalid context\n"))
+		ret = -EBUSY;
+	else if (crypto_aead_get_flags(aead) & CRYPTO_TFM_NEED_KEY)
 		ret = -ENOKEY;
 	else if (req->cryptlen < crypto_aead_authsize(aead))
 		ret = -EINVAL;