Skip to content

Commit 6e3b671

Browse files
ardbiesheuvelgregkh
authored andcommitted
crypto: aesni - prevent misaligned buffers on the stack
commit a13ed1d upstream. The GCM mode driver uses 16 byte aligned buffers on the stack to pass the IV to the asm helpers, but unfortunately, the x86 port does not guarantee that the stack pointer is 16 byte aligned upon entry in the first place. Since the compiler is not aware of this, it will not emit the additional stack realignment sequence that is needed, and so the alignment is not guaranteed to be more than 8 bytes. So instead, allocate some padding on the stack, and realign the IV pointer by hand. Cc: <stable@vger.kernel.org> Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e2c540e commit 6e3b671

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

‎arch/x86/crypto/aesni-intel_glue.c‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,8 @@ static int gcmaes_crypt_by_sg(bool enc, struct aead_request *req,
686686
struct crypto_aead *tfm = crypto_aead_reqtfm(req);
687687
unsigned long auth_tag_len = crypto_aead_authsize(tfm);
688688
const struct aesni_gcm_tfm_s *gcm_tfm = aesni_gcm_tfm;
689-
struct gcm_context_data data AESNI_ALIGN_ATTR;
689+
u8 databuf[sizeof(struct gcm_context_data) + (AESNI_ALIGN - 8)] __aligned(8);
690+
struct gcm_context_data *data = PTR_ALIGN((void *)databuf, AESNI_ALIGN);
690691
struct scatter_walk dst_sg_walk = {};
691692
unsigned long left = req->cryptlen;
692693
unsigned long len, srclen, dstlen;
@@ -735,8 +736,7 @@ static int gcmaes_crypt_by_sg(bool enc, struct aead_request *req,
735736
}
736737

737738
kernel_fpu_begin();
738-
gcm_tfm->init(aes_ctx, &data, iv,
739-
hash_subkey, assoc, assoclen);
739+
gcm_tfm->init(aes_ctx, data, iv, hash_subkey, assoc, assoclen);
740740
if (req->src != req->dst) {
741741
while (left) {
742742
src = scatterwalk_map(&src_sg_walk);
@@ -746,10 +746,10 @@ static int gcmaes_crypt_by_sg(bool enc, struct aead_request *req,
746746
len = min(srclen, dstlen);
747747
if (len) {
748748
if (enc)
749-
gcm_tfm->enc_update(aes_ctx, &data,
749+
gcm_tfm->enc_update(aes_ctx, data,
750750
dst, src, len);
751751
else
752-
gcm_tfm->dec_update(aes_ctx, &data,
752+
gcm_tfm->dec_update(aes_ctx, data,
753753
dst, src, len);
754754
}
755755
left -= len;
@@ -767,10 +767,10 @@ static int gcmaes_crypt_by_sg(bool enc, struct aead_request *req,
767767
len = scatterwalk_clamp(&src_sg_walk, left);
768768
if (len) {
769769
if (enc)
770-
gcm_tfm->enc_update(aes_ctx, &data,
770+
gcm_tfm->enc_update(aes_ctx, data,
771771
src, src, len);
772772
else
773-
gcm_tfm->dec_update(aes_ctx, &data,
773+
gcm_tfm->dec_update(aes_ctx, data,
774774
src, src, len);
775775
}
776776
left -= len;
@@ -779,7 +779,7 @@ static int gcmaes_crypt_by_sg(bool enc, struct aead_request *req,
779779
scatterwalk_done(&src_sg_walk, 1, left);
780780
}
781781
}
782-
gcm_tfm->finalize(aes_ctx, &data, authTag, auth_tag_len);
782+
gcm_tfm->finalize(aes_ctx, data, authTag, auth_tag_len);
783783
kernel_fpu_end();
784784

785785
if (!assocmem)
@@ -828,7 +828,8 @@ static int helper_rfc4106_encrypt(struct aead_request *req)
828828
struct crypto_aead *tfm = crypto_aead_reqtfm(req);
829829
struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
830830
void *aes_ctx = &(ctx->aes_key_expanded);
831-
u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
831+
u8 ivbuf[16 + (AESNI_ALIGN - 8)] __aligned(8);
832+
u8 *iv = PTR_ALIGN(&ivbuf[0], AESNI_ALIGN);
832833
unsigned int i;
833834
__be32 counter = cpu_to_be32(1);
834835

@@ -855,7 +856,8 @@ static int helper_rfc4106_decrypt(struct aead_request *req)
855856
struct crypto_aead *tfm = crypto_aead_reqtfm(req);
856857
struct aesni_rfc4106_gcm_ctx *ctx = aesni_rfc4106_gcm_ctx_get(tfm);
857858
void *aes_ctx = &(ctx->aes_key_expanded);
858-
u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
859+
u8 ivbuf[16 + (AESNI_ALIGN - 8)] __aligned(8);
860+
u8 *iv = PTR_ALIGN(&ivbuf[0], AESNI_ALIGN);
859861
unsigned int i;
860862

861863
if (unlikely(req->assoclen != 16 && req->assoclen != 20))
@@ -985,7 +987,8 @@ static int generic_gcmaes_encrypt(struct aead_request *req)
985987
struct crypto_aead *tfm = crypto_aead_reqtfm(req);
986988
struct generic_gcmaes_ctx *ctx = generic_gcmaes_ctx_get(tfm);
987989
void *aes_ctx = &(ctx->aes_key_expanded);
988-
u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
990+
u8 ivbuf[16 + (AESNI_ALIGN - 8)] __aligned(8);
991+
u8 *iv = PTR_ALIGN(&ivbuf[0], AESNI_ALIGN);
989992
__be32 counter = cpu_to_be32(1);
990993

991994
memcpy(iv, req->iv, 12);
@@ -1001,7 +1004,8 @@ static int generic_gcmaes_decrypt(struct aead_request *req)
10011004
struct crypto_aead *tfm = crypto_aead_reqtfm(req);
10021005
struct generic_gcmaes_ctx *ctx = generic_gcmaes_ctx_get(tfm);
10031006
void *aes_ctx = &(ctx->aes_key_expanded);
1004-
u8 iv[16] __attribute__ ((__aligned__(AESNI_ALIGN)));
1007+
u8 ivbuf[16 + (AESNI_ALIGN - 8)] __aligned(8);
1008+
u8 *iv = PTR_ALIGN(&ivbuf[0], AESNI_ALIGN);
10051009

10061010
memcpy(iv, req->iv, 12);
10071011
*((__be32 *)(iv+12)) = counter;

0 commit comments

Comments
 (0)