Skip to content

Commit 652a301

Browse files
EricccTaiwanEric Biggers
authored andcommitted
crypto: arm64/aes-neonbs - Move key expansion off the stack
aesbs_setkey() and aesbs_cbc_ctr_setkey() allocate struct crypto_aes_ctx on the stack. On arm64, the kernel-mode NEON context is also stored on the stack, causing the combined frame size to exceed 1024 bytes and triggering -Wframe-larger-than= warnings. Allocate struct crypto_aes_ctx on the heap instead and use kfree_sensitive() to ensure the key material is zeroed on free. Use a goto-based cleanup path to ensure kfree_sensitive() is always called. Signed-off-by: Cheng-Yang Chou <[email protected]> Fixes: 4fa617c ("arm64/fpsimd: Allocate kernel mode FP/SIMD buffers on the stack") Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Eric Biggers <[email protected]>
1 parent 1f318b9 commit 652a301

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

arch/arm64/crypto/aes-neonbs-glue.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,24 @@ static int aesbs_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
7676
unsigned int key_len)
7777
{
7878
struct aesbs_ctx *ctx = crypto_skcipher_ctx(tfm);
79-
struct crypto_aes_ctx rk;
79+
struct crypto_aes_ctx *rk;
8080
int err;
8181

82-
err = aes_expandkey(&rk, in_key, key_len);
82+
rk = kmalloc(sizeof(*rk), GFP_KERNEL);
83+
if (!rk)
84+
return -ENOMEM;
85+
86+
err = aes_expandkey(rk, in_key, key_len);
8387
if (err)
84-
return err;
88+
goto out;
8589

8690
ctx->rounds = 6 + key_len / 4;
8791

8892
scoped_ksimd()
89-
aesbs_convert_key(ctx->rk, rk.key_enc, ctx->rounds);
90-
91-
return 0;
93+
aesbs_convert_key(ctx->rk, rk->key_enc, ctx->rounds);
94+
out:
95+
kfree_sensitive(rk);
96+
return err;
9297
}
9398

9499
static int __ecb_crypt(struct skcipher_request *req,
@@ -133,22 +138,26 @@ static int aesbs_cbc_ctr_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
133138
unsigned int key_len)
134139
{
135140
struct aesbs_cbc_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
136-
struct crypto_aes_ctx rk;
141+
struct crypto_aes_ctx *rk;
137142
int err;
138143

139-
err = aes_expandkey(&rk, in_key, key_len);
144+
rk = kmalloc(sizeof(*rk), GFP_KERNEL);
145+
if (!rk)
146+
return -ENOMEM;
147+
148+
err = aes_expandkey(rk, in_key, key_len);
140149
if (err)
141-
return err;
150+
goto out;
142151

143152
ctx->key.rounds = 6 + key_len / 4;
144153

145-
memcpy(ctx->enc, rk.key_enc, sizeof(ctx->enc));
154+
memcpy(ctx->enc, rk->key_enc, sizeof(ctx->enc));
146155

147156
scoped_ksimd()
148-
aesbs_convert_key(ctx->key.rk, rk.key_enc, ctx->key.rounds);
149-
memzero_explicit(&rk, sizeof(rk));
150-
151-
return 0;
157+
aesbs_convert_key(ctx->key.rk, rk->key_enc, ctx->key.rounds);
158+
out:
159+
kfree_sensitive(rk);
160+
return err;
152161
}
153162

154163
static int cbc_encrypt(struct skcipher_request *req)

0 commit comments

Comments
 (0)