linux-crypto.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] x86 AEGIS fixes
@ 2025-07-08 19:38 Eric Biggers
  2025-07-08 19:38 ` [PATCH 1/2] crypto: x86/aegis - Fix sleeping when disallowed on PREEMPT_RT Eric Biggers
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Eric Biggers @ 2025-07-08 19:38 UTC (permalink / raw)
  To: linux-crypto; +Cc: linux-kernel, x86, linux-rt-devel, Eric Biggers

Since the legacy crypto API likes to randomly allocate memory, the x86
AEGIS code has some bugs.  This series fixes them.

Eric Biggers (2):
  crypto: x86/aegis - Fix sleeping when disallowed on PREEMPT_RT
  crypto: x86/aegis - Add missing error checks

 arch/x86/crypto/aegis128-aesni-glue.c | 40 +++++++++++++++++++--------
 1 file changed, 29 insertions(+), 11 deletions(-)


base-commit: 181698af38d3f93381229ad89c09b5bd0496661a
-- 
2.50.0


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

* [PATCH 1/2] crypto: x86/aegis - Fix sleeping when disallowed on PREEMPT_RT
  2025-07-08 19:38 [PATCH 0/2] x86 AEGIS fixes Eric Biggers
@ 2025-07-08 19:38 ` Eric Biggers
  2025-07-08 19:38 ` [PATCH 2/2] crypto: x86/aegis - Add missing error checks Eric Biggers
  2025-07-18 11:11 ` [PATCH 0/2] x86 AEGIS fixes Herbert Xu
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Biggers @ 2025-07-08 19:38 UTC (permalink / raw)
  To: linux-crypto; +Cc: linux-kernel, x86, linux-rt-devel, Eric Biggers, stable

skcipher_walk_done() can call kfree(), which takes a spinlock, which
makes it incorrect to call while preemption is disabled on PREEMPT_RT.
Therefore, end the kernel-mode FPU section before calling
skcipher_walk_done(), and restart it afterwards.

Moreover, pass atomic=false to skcipher_walk_aead_encrypt() instead of
atomic=true.  The point of atomic=true was to make skcipher_walk_done()
safe to call while in a kernel-mode FPU section, but that does not
actually work.  So just use the usual atomic=false.

Fixes: 1d373d4e8e15 ("crypto: x86 - Add optimized AEGIS implementations")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 arch/x86/crypto/aegis128-aesni-glue.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/arch/x86/crypto/aegis128-aesni-glue.c b/arch/x86/crypto/aegis128-aesni-glue.c
index f1b6d40154e35..3cb5c193038bb 100644
--- a/arch/x86/crypto/aegis128-aesni-glue.c
+++ b/arch/x86/crypto/aegis128-aesni-glue.c
@@ -117,11 +117,13 @@ crypto_aegis128_aesni_process_crypt(struct aegis_state *state,
 		else
 			aegis128_aesni_dec(state, walk->src.virt.addr,
 					   walk->dst.virt.addr,
 					   round_down(walk->nbytes,
 						      AEGIS128_BLOCK_SIZE));
+		kernel_fpu_end();
 		skcipher_walk_done(walk, walk->nbytes % AEGIS128_BLOCK_SIZE);
+		kernel_fpu_begin();
 	}
 
 	if (walk->nbytes) {
 		if (enc)
 			aegis128_aesni_enc_tail(state, walk->src.virt.addr,
@@ -129,11 +131,13 @@ crypto_aegis128_aesni_process_crypt(struct aegis_state *state,
 						walk->nbytes);
 		else
 			aegis128_aesni_dec_tail(state, walk->src.virt.addr,
 						walk->dst.virt.addr,
 						walk->nbytes);
+		kernel_fpu_end();
 		skcipher_walk_done(walk, 0);
+		kernel_fpu_begin();
 	}
 }
 
 static struct aegis_ctx *crypto_aegis128_aesni_ctx(struct crypto_aead *aead)
 {
@@ -174,13 +178,13 @@ crypto_aegis128_aesni_crypt(struct aead_request *req,
 	struct aegis_ctx *ctx = crypto_aegis128_aesni_ctx(tfm);
 	struct skcipher_walk walk;
 	struct aegis_state state;
 
 	if (enc)
-		skcipher_walk_aead_encrypt(&walk, req, true);
+		skcipher_walk_aead_encrypt(&walk, req, false);
 	else
-		skcipher_walk_aead_decrypt(&walk, req, true);
+		skcipher_walk_aead_decrypt(&walk, req, false);
 
 	kernel_fpu_begin();
 
 	aegis128_aesni_init(&state, &ctx->key, req->iv);
 	crypto_aegis128_aesni_process_ad(&state, req->src, req->assoclen);
-- 
2.50.0


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

* [PATCH 2/2] crypto: x86/aegis - Add missing error checks
  2025-07-08 19:38 [PATCH 0/2] x86 AEGIS fixes Eric Biggers
  2025-07-08 19:38 ` [PATCH 1/2] crypto: x86/aegis - Fix sleeping when disallowed on PREEMPT_RT Eric Biggers
@ 2025-07-08 19:38 ` Eric Biggers
  2025-07-18 11:11 ` [PATCH 0/2] x86 AEGIS fixes Herbert Xu
  2 siblings, 0 replies; 4+ messages in thread
From: Eric Biggers @ 2025-07-08 19:38 UTC (permalink / raw)
  To: linux-crypto; +Cc: linux-kernel, x86, linux-rt-devel, Eric Biggers, stable

The skcipher_walk functions can allocate memory and can fail, so
checking for errors is necessary.

Fixes: 1d373d4e8e15 ("crypto: x86 - Add optimized AEGIS implementations")
Cc: stable@vger.kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
 arch/x86/crypto/aegis128-aesni-glue.c | 36 +++++++++++++++++++--------
 1 file changed, 25 insertions(+), 11 deletions(-)

diff --git a/arch/x86/crypto/aegis128-aesni-glue.c b/arch/x86/crypto/aegis128-aesni-glue.c
index 3cb5c193038bb..f1adfba1a76ea 100644
--- a/arch/x86/crypto/aegis128-aesni-glue.c
+++ b/arch/x86/crypto/aegis128-aesni-glue.c
@@ -102,14 +102,16 @@ static void crypto_aegis128_aesni_process_ad(
 		memset(buf.bytes + pos, 0, AEGIS128_BLOCK_SIZE - pos);
 		aegis128_aesni_ad(state, buf.bytes, AEGIS128_BLOCK_SIZE);
 	}
 }
 
-static __always_inline void
+static __always_inline int
 crypto_aegis128_aesni_process_crypt(struct aegis_state *state,
 				    struct skcipher_walk *walk, bool enc)
 {
+	int err = 0;
+
 	while (walk->nbytes >= AEGIS128_BLOCK_SIZE) {
 		if (enc)
 			aegis128_aesni_enc(state, walk->src.virt.addr,
 					   walk->dst.virt.addr,
 					   round_down(walk->nbytes,
@@ -118,11 +120,12 @@ crypto_aegis128_aesni_process_crypt(struct aegis_state *state,
 			aegis128_aesni_dec(state, walk->src.virt.addr,
 					   walk->dst.virt.addr,
 					   round_down(walk->nbytes,
 						      AEGIS128_BLOCK_SIZE));
 		kernel_fpu_end();
-		skcipher_walk_done(walk, walk->nbytes % AEGIS128_BLOCK_SIZE);
+		err = skcipher_walk_done(walk,
+					 walk->nbytes % AEGIS128_BLOCK_SIZE);
 		kernel_fpu_begin();
 	}
 
 	if (walk->nbytes) {
 		if (enc)
@@ -132,13 +135,14 @@ crypto_aegis128_aesni_process_crypt(struct aegis_state *state,
 		else
 			aegis128_aesni_dec_tail(state, walk->src.virt.addr,
 						walk->dst.virt.addr,
 						walk->nbytes);
 		kernel_fpu_end();
-		skcipher_walk_done(walk, 0);
+		err = skcipher_walk_done(walk, 0);
 		kernel_fpu_begin();
 	}
+	return err;
 }
 
 static struct aegis_ctx *crypto_aegis128_aesni_ctx(struct crypto_aead *aead)
 {
 	u8 *ctx = crypto_aead_ctx(aead);
@@ -167,43 +171,50 @@ static int crypto_aegis128_aesni_setauthsize(struct crypto_aead *tfm,
 	if (authsize < AEGIS128_MIN_AUTH_SIZE)
 		return -EINVAL;
 	return 0;
 }
 
-static __always_inline void
+static __always_inline int
 crypto_aegis128_aesni_crypt(struct aead_request *req,
 			    struct aegis_block *tag_xor,
 			    unsigned int cryptlen, bool enc)
 {
 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
 	struct aegis_ctx *ctx = crypto_aegis128_aesni_ctx(tfm);
 	struct skcipher_walk walk;
 	struct aegis_state state;
+	int err;
 
 	if (enc)
-		skcipher_walk_aead_encrypt(&walk, req, false);
+		err = skcipher_walk_aead_encrypt(&walk, req, false);
 	else
-		skcipher_walk_aead_decrypt(&walk, req, false);
+		err = skcipher_walk_aead_decrypt(&walk, req, false);
+	if (err)
+		return err;
 
 	kernel_fpu_begin();
 
 	aegis128_aesni_init(&state, &ctx->key, req->iv);
 	crypto_aegis128_aesni_process_ad(&state, req->src, req->assoclen);
-	crypto_aegis128_aesni_process_crypt(&state, &walk, enc);
-	aegis128_aesni_final(&state, tag_xor, req->assoclen, cryptlen);
-
+	err = crypto_aegis128_aesni_process_crypt(&state, &walk, enc);
+	if (err == 0)
+		aegis128_aesni_final(&state, tag_xor, req->assoclen, cryptlen);
 	kernel_fpu_end();
+	return err;
 }
 
 static int crypto_aegis128_aesni_encrypt(struct aead_request *req)
 {
 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
 	struct aegis_block tag = {};
 	unsigned int authsize = crypto_aead_authsize(tfm);
 	unsigned int cryptlen = req->cryptlen;
+	int err;
 
-	crypto_aegis128_aesni_crypt(req, &tag, cryptlen, true);
+	err = crypto_aegis128_aesni_crypt(req, &tag, cryptlen, true);
+	if (err)
+		return err;
 
 	scatterwalk_map_and_copy(tag.bytes, req->dst,
 				 req->assoclen + cryptlen, authsize, 1);
 	return 0;
 }
@@ -214,15 +225,18 @@ static int crypto_aegis128_aesni_decrypt(struct aead_request *req)
 
 	struct crypto_aead *tfm = crypto_aead_reqtfm(req);
 	struct aegis_block tag;
 	unsigned int authsize = crypto_aead_authsize(tfm);
 	unsigned int cryptlen = req->cryptlen - authsize;
+	int err;
 
 	scatterwalk_map_and_copy(tag.bytes, req->src,
 				 req->assoclen + cryptlen, authsize, 0);
 
-	crypto_aegis128_aesni_crypt(req, &tag, cryptlen, false);
+	err = crypto_aegis128_aesni_crypt(req, &tag, cryptlen, false);
+	if (err)
+		return err;
 
 	return crypto_memneq(tag.bytes, zeros.bytes, authsize) ? -EBADMSG : 0;
 }
 
 static struct aead_alg crypto_aegis128_aesni_alg = {
-- 
2.50.0


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

* Re: [PATCH 0/2] x86 AEGIS fixes
  2025-07-08 19:38 [PATCH 0/2] x86 AEGIS fixes Eric Biggers
  2025-07-08 19:38 ` [PATCH 1/2] crypto: x86/aegis - Fix sleeping when disallowed on PREEMPT_RT Eric Biggers
  2025-07-08 19:38 ` [PATCH 2/2] crypto: x86/aegis - Add missing error checks Eric Biggers
@ 2025-07-18 11:11 ` Herbert Xu
  2 siblings, 0 replies; 4+ messages in thread
From: Herbert Xu @ 2025-07-18 11:11 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-crypto, linux-kernel, x86, linux-rt-devel, ebiggers

Eric Biggers <ebiggers@kernel.org> wrote:
> Since the legacy crypto API likes to randomly allocate memory, the x86
> AEGIS code has some bugs.  This series fixes them.
> 
> Eric Biggers (2):
>  crypto: x86/aegis - Fix sleeping when disallowed on PREEMPT_RT
>  crypto: x86/aegis - Add missing error checks
> 
> arch/x86/crypto/aegis128-aesni-glue.c | 40 +++++++++++++++++++--------
> 1 file changed, 29 insertions(+), 11 deletions(-)
> 
> 
> base-commit: 181698af38d3f93381229ad89c09b5bd0496661a

All 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

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

end of thread, other threads:[~2025-07-18 11:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-08 19:38 [PATCH 0/2] x86 AEGIS fixes Eric Biggers
2025-07-08 19:38 ` [PATCH 1/2] crypto: x86/aegis - Fix sleeping when disallowed on PREEMPT_RT Eric Biggers
2025-07-08 19:38 ` [PATCH 2/2] crypto: x86/aegis - Add missing error checks Eric Biggers
2025-07-18 11:11 ` [PATCH 0/2] x86 AEGIS fixes Herbert Xu

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