Skip to contentSkip to conversation

crypto: arm64/aes-neonbs - Move key expansion off the stack

PATCH Superseded Cheng-Yang Chou 2 participants 5 messages Mar 5 – Mar 6

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover crypto: arm64/aes-neonbs - Move key expansion off the stack

aesbs_setkey() and aesbs_cbc_ctr_setkey() trigger -Wframe-larger-than= warnings because struct crypto_aes_ctx is allocated on the stack, pushing the frame size to ~1040 bytes and exceeding the 1024-byte limit.

arch/arm64/crypto/aes-neonbs-glue.c: In function ‘aesbs_setkey’:
arch/arm64/crypto/aes-neonbs-glue.c:92:1: warning: the frame size of 1040 bytes is larger than 1024 bytes [-Wframe-larger-than=]
92 | }

      | ^
arch/arm64/crypto/aes-neonbs-glue.c: In function ‘aesbs_cbc_ctr_setkey’:
arch/arm64/crypto/aes-neonbs-glue.c:152:1: warning: the frame size of 1040 bytes is larger than 1024 bytes [-Wframe-larger-than=]
  152 | }
      | ^

Tested on arm64. Confirmed the -Wframe-larger-than= warning is resolved.

Thanks,
Cheng-Yang

---

Cheng-Yang Chou (1):
crypto: arm64/aes-neonbs - Move key expansion off the stack

 arch/arm64/crypto/aes-neonbs-glue.c | 39 ++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 14 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH 1/1 crypto: arm64/aes-neonbs - Move key expansion off the stack

aesbs_setkey() and aesbs_cbc_ctr_setkey() trigger -Wframe-larger-than= warnings due to struct crypto_aes_ctx being allocated on the stack, causing the frame size to exceed 1024 bytes.

Allocate struct crypto_aes_ctx on the heap instead to reduce stack usage. Use a goto-based cleanup path to ensure memzero_explicit() and kfree() are always called.

---
 arch/arm64/crypto/aes-neonbs-glue.c | 39 ++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 14 deletions(-)
arch/arm64/crypto/aes-neonbs-glue.c +25 -14
index cb87c8fc66b3..a24b66fd5cad 100644
@@ -76,19 +76,25 @@ static int aesbs_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
 			unsigned int key_len)
 {
 	struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm);
-	struct crypto_aes_ctx rk;
+	struct crypto_aes_ctx *rk;
 	int err;
 
-	err = aes_expandkey(&rk, in_key, key_len);
+	rk = kmalloc(sizeof(*rk), GFP_KERNEL);
+	if (!rk)
+		return -ENOMEM;
+
+	err = aes_expandkey(rk, in_key, key_len);
 	if (err)
-		return err;
+		goto out;
 
 	ctx->rounds = 6 + key_len / 4;
 
 	scoped_ksimd()
-		aesbs_convert_key(ctx->rk, rk.key_enc, ctx->rounds);
-
-	return 0;
+		aesbs_convert_key(ctx->rk, rk->key_enc, ctx->rounds);
+out:
+	memzero_explicit(rk, sizeof(*rk));
+	kfree(rk);
+	return err;
 }
 
 static int __ecb_crypt(struct skcipher_request *req,
@@ -133,22 +139,27 @@ static int aesbs_cbc_ctr_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
 			    unsigned int key_len)
 {
 	struct aesbs_cbc_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
-	struct crypto_aes_ctx rk;
+	struct crypto_aes_ctx *rk;
 	int err;
 
-	err = aes_expandkey(&rk, in_key, key_len);
+	rk = kmalloc(sizeof(*rk), GFP_KERNEL);
+	if (!rk)
+		return -ENOMEM;
+
+	err = aes_expandkey(rk, in_key, key_len);
 	if (err)
-		return err;
+		goto out;
 
 	ctx->key.rounds = 6 + key_len / 4;
 
-	memcpy(ctx->enc, rk.key_enc, sizeof(ctx->enc));
+	memcpy(ctx->enc, rk->key_enc, sizeof(ctx->enc));
 
 	scoped_ksimd()
-		aesbs_convert_key(ctx->key.rk, rk.key_enc, ctx->key.rounds);
-	memzero_explicit(&rk, sizeof(rk));
-
-	return 0;
+		aesbs_convert_key(ctx->key.rk, rk->key_enc, ctx->key.rounds);
+out:
+	memzero_explicit(rk, sizeof(*rk));
+	kfree(rk);
+	return err;
 }
 
 static int cbc_encrypt(struct skcipher_request *req)
Signature · 1 line
2.48.1
#2 lore raw
Quoted 73 lines · Cheng-Yang Chou

aesbs_setkey() and aesbs_cbc_ctr_setkey() trigger -Wframe-larger-than= warnings due to struct crypto_aes_ctx being allocated on the stack, causing the frame size to exceed 1024 bytes.

Allocate struct crypto_aes_ctx on the heap instead to reduce stack usage. Use a goto-based cleanup path to ensure memzero_explicit() and kfree() are always called.

---
 arch/arm64/crypto/aes-neonbs-glue.c | 39 ++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 14 deletions(-)
arch/arm64/crypto/aes-neonbs-glue.c +20 -10
index cb87c8fc66b3..a24b66fd5cad 100644
@@ -76,19 +76,25 @@ static int aesbs_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
 			unsigned int key_len)
 {
 	struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm);
-	struct crypto_aes_ctx rk;
+	struct crypto_aes_ctx *rk;
 	int err;
 
-	err = aes_expandkey(&rk, in_key, key_len);
+	rk = kmalloc(sizeof(*rk), GFP_KERNEL);
+	if (!rk)
+		return -ENOMEM;
+
+	err = aes_expandkey(rk, in_key, key_len);
 	if (err)
-		return err;
+		goto out;
 
 	ctx->rounds = 6 + key_len / 4;
 
 	scoped_ksimd()
-		aesbs_convert_key(ctx->rk, rk.key_enc, ctx->rounds);
-
-	return 0;
+		aesbs_convert_key(ctx->rk, rk->key_enc, ctx->rounds);
+out:
+	memzero_explicit(rk, sizeof(*rk));
+	kfree(rk);
+	return err;
 }
 
 static int __ecb_crypt(struct skcipher_request *req,
@@ -133,22 +139,27 @@ static int aesbs_cbc_ctr_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
 			    unsigned int key_len)
 {
 	struct aesbs_cbc_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
-	struct crypto_aes_ctx rk;
+	struct crypto_aes_ctx *rk;
 	int err;
 
-	err = aes_expandkey(&rk, in_key, key_len);
+	rk = kmalloc(sizeof(*rk), GFP_KERNEL);
+	if (!rk)
+		return -ENOMEM;
+
+	err = aes_expandkey(rk, in_key, key_len);
 	if (err)
-		return err;
+		goto out;
 
 	ctx->key.rounds = 6 + key_len / 4;
 
-	memcpy(ctx->enc, rk.key_enc, sizeof(ctx->enc));
+	memcpy(ctx->enc, rk->key_enc, sizeof(ctx->enc));
 
 	scoped_ksimd()

arch/arm64/crypto/aes-neonbs-glue.c static int aesbs_cbc_ctr_setkey(struct crypto_skcipher *tfm, const u8 *in_key, · line 163

Quoted 10 lines
arch/arm64/crypto/aes-neonbs-glue.c +5 -4
-		aesbs_convert_key(ctx->key.rk, rk.key_enc, ctx->key.rounds);
-	memzero_explicit(&rk, sizeof(rk));
-
-	return 0;
+		aesbs_convert_key(ctx->key.rk, rk->key_enc, ctx->key.rounds);
+out:
+	memzero_explicit(rk, sizeof(*rk));
+	kfree(rk);
+	return err;
 }

Instead of memzero_explicit() followed by kfree(), just use kfree_sensitive().

Also, single patches should not have a cover letter. Just send a single patch email with all the details in the patch itself.

As for the actual change, I guess it's okay for now. Ideally we'd refactor the aes-bs key preparation to not need temporary space.

- Eric

#3 lore raw

Hi Eric,

Quoted 8 lines · Eric Biggers

Instead of memzero_explicit() followed by kfree(), just use kfree_sensitive().

Also, single patches should not have a cover letter. Just send a single patch email with all the details in the patch itself.

As for the actual change, I guess it's okay for now. Ideally we'd refactor the aes-bs key preparation to not need temporary space.

Thanks for the feedback.
I'll send a v2 to address your comments.

The arm implementation also allocates struct crypto_aes_ctx on the stack in aesbs_setkey(). Should I include a fix for it as well? Note that I can only test on arm64.

Also, I'd be happy to help with the refactoring if you can point me in the right direction.

Signature · 2 lines
Thanks,
Cheng-Yang
#4 lore raw
Quoted 15 lines · Cheng-Yang Chou

Hi Eric,

Quoted 8 lines · Eric Biggers

Instead of memzero_explicit() followed by kfree(), just use kfree_sensitive().

Also, single patches should not have a cover letter. Just send a single patch email with all the details in the patch itself.

As for the actual change, I guess it's okay for now. Ideally we'd refactor the aes-bs key preparation to not need temporary space.

Thanks for the feedback.
I'll send a v2 to address your comments.

Quoted 6 lines

The arm implementation also allocates struct crypto_aes_ctx on the stack in aesbs_setkey(). Should I include a fix for it as well? Note that I can only test on arm64.

Also, I'd be happy to help with the refactoring if you can point me in the right direction.

arm doesn't store the kernel-mode NEON context on the stack, so a similar change shouldn't be needed there. This issue showed up only because arm64 started doing that, which made the stack memory used by aesbs_setkey() exceed ~1000 bytes due to the crypto_aes_ctx and the kernel-mode NEON context each using about 500.

- Eric