Skip to content

Commit 1ecdd37

Browse files
Junaid Shahidherbertx
authored andcommitted
crypto: aesni - Fix out-of-bounds access of the AAD buffer in generic-gcm-aesni
The aesni_gcm_enc/dec functions can access memory after the end of the AAD buffer if the AAD length is not a multiple of 4 bytes. It didn't matter with rfc4106-gcm-aesni as in that case the AAD was always followed by the 8 byte IV, but that is no longer the case with generic-gcm-aesni. This can potentially result in accessing a page that is not mapped and thus causing the machine to crash. This patch fixes that by reading the last <16 byte block of the AAD byte-by-byte and optionally via an 8-byte load if the block was at least 8 bytes. Fixes: 0487cca ("crypto: aesni - make non-AVX AES-GCM work with any aadlen") Cc: <stable@vger.kernel.org> Signed-off-by: Junaid Shahid <junaids@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent b20209c commit 1ecdd37

1 file changed

Lines changed: 12 additions & 100 deletions

File tree

‎arch/x86/crypto/aesni-intel_asm.S‎

Lines changed: 12 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,6 @@ SHIFT_MASK: .octa 0x0f0e0d0c0b0a09080706050403020100
8989
ALL_F: .octa 0xffffffffffffffffffffffffffffffff
9090
.octa 0x00000000000000000000000000000000
9191

92-
.section .rodata
93-
.align 16
94-
.type aad_shift_arr, @object
95-
.size aad_shift_arr, 272
96-
aad_shift_arr:
97-
.octa 0xffffffffffffffffffffffffffffffff
98-
.octa 0xffffffffffffffffffffffffffffff0C
99-
.octa 0xffffffffffffffffffffffffffff0D0C
100-
.octa 0xffffffffffffffffffffffffff0E0D0C
101-
.octa 0xffffffffffffffffffffffff0F0E0D0C
102-
.octa 0xffffffffffffffffffffff0C0B0A0908
103-
.octa 0xffffffffffffffffffff0D0C0B0A0908
104-
.octa 0xffffffffffffffffff0E0D0C0B0A0908
105-
.octa 0xffffffffffffffff0F0E0D0C0B0A0908
106-
.octa 0xffffffffffffff0C0B0A090807060504
107-
.octa 0xffffffffffff0D0C0B0A090807060504
108-
.octa 0xffffffffff0E0D0C0B0A090807060504
109-
.octa 0xffffffff0F0E0D0C0B0A090807060504
110-
.octa 0xffffff0C0B0A09080706050403020100
111-
.octa 0xffff0D0C0B0A09080706050403020100
112-
.octa 0xff0E0D0C0B0A09080706050403020100
113-
.octa 0x0F0E0D0C0B0A09080706050403020100
114-
115-
11692
.text
11793

11894

@@ -303,62 +279,30 @@ _done_read_partial_block_\@:
303279
XMM2 XMM3 XMM4 XMMDst TMP6 TMP7 i i_seq operation
304280
MOVADQ SHUF_MASK(%rip), %xmm14
305281
mov arg7, %r10 # %r10 = AAD
306-
mov arg8, %r12 # %r12 = aadLen
307-
mov %r12, %r11
282+
mov arg8, %r11 # %r11 = aadLen
308283
pxor %xmm\i, %xmm\i
309284
pxor \XMM2, \XMM2
310285

311286
cmp $16, %r11
312-
jl _get_AAD_rest8\num_initial_blocks\operation
287+
jl _get_AAD_rest\num_initial_blocks\operation
313288
_get_AAD_blocks\num_initial_blocks\operation:
314289
movdqu (%r10), %xmm\i
315290
PSHUFB_XMM %xmm14, %xmm\i # byte-reflect the AAD data
316291
pxor %xmm\i, \XMM2
317292
GHASH_MUL \XMM2, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
318293
add $16, %r10
319-
sub $16, %r12
320294
sub $16, %r11
321295
cmp $16, %r11
322296
jge _get_AAD_blocks\num_initial_blocks\operation
323297

324298
movdqu \XMM2, %xmm\i
299+
300+
/* read the last <16B of AAD */
301+
_get_AAD_rest\num_initial_blocks\operation:
325302
cmp $0, %r11
326303
je _get_AAD_done\num_initial_blocks\operation
327304

328-
pxor %xmm\i,%xmm\i
329-
330-
/* read the last <16B of AAD. since we have at least 4B of
331-
data right after the AAD (the ICV, and maybe some CT), we can
332-
read 4B/8B blocks safely, and then get rid of the extra stuff */
333-
_get_AAD_rest8\num_initial_blocks\operation:
334-
cmp $4, %r11
335-
jle _get_AAD_rest4\num_initial_blocks\operation
336-
movq (%r10), \TMP1
337-
add $8, %r10
338-
sub $8, %r11
339-
pslldq $8, \TMP1
340-
psrldq $8, %xmm\i
341-
pxor \TMP1, %xmm\i
342-
jmp _get_AAD_rest8\num_initial_blocks\operation
343-
_get_AAD_rest4\num_initial_blocks\operation:
344-
cmp $0, %r11
345-
jle _get_AAD_rest0\num_initial_blocks\operation
346-
mov (%r10), %eax
347-
movq %rax, \TMP1
348-
add $4, %r10
349-
sub $4, %r10
350-
pslldq $12, \TMP1
351-
psrldq $4, %xmm\i
352-
pxor \TMP1, %xmm\i
353-
_get_AAD_rest0\num_initial_blocks\operation:
354-
/* finalize: shift out the extra bytes we read, and align
355-
left. since pslldq can only shift by an immediate, we use
356-
vpshufb and an array of shuffle masks */
357-
movq %r12, %r11
358-
salq $4, %r11
359-
movdqu aad_shift_arr(%r11), \TMP1
360-
PSHUFB_XMM \TMP1, %xmm\i
361-
_get_AAD_rest_final\num_initial_blocks\operation:
305+
READ_PARTIAL_BLOCK %r10, %r11, \TMP1, %xmm\i
362306
PSHUFB_XMM %xmm14, %xmm\i # byte-reflect the AAD data
363307
pxor \XMM2, %xmm\i
364308
GHASH_MUL %xmm\i, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
@@ -562,62 +506,30 @@ _initial_blocks_done\num_initial_blocks\operation:
562506
XMM2 XMM3 XMM4 XMMDst TMP6 TMP7 i i_seq operation
563507
MOVADQ SHUF_MASK(%rip), %xmm14
564508
mov arg7, %r10 # %r10 = AAD
565-
mov arg8, %r12 # %r12 = aadLen
566-
mov %r12, %r11
509+
mov arg8, %r11 # %r11 = aadLen
567510
pxor %xmm\i, %xmm\i
568511
pxor \XMM2, \XMM2
569512

570513
cmp $16, %r11
571-
jl _get_AAD_rest8\num_initial_blocks\operation
514+
jl _get_AAD_rest\num_initial_blocks\operation
572515
_get_AAD_blocks\num_initial_blocks\operation:
573516
movdqu (%r10), %xmm\i
574517
PSHUFB_XMM %xmm14, %xmm\i # byte-reflect the AAD data
575518
pxor %xmm\i, \XMM2
576519
GHASH_MUL \XMM2, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1
577520
add $16, %r10
578-
sub $16, %r12
579521
sub $16, %r11
580522
cmp $16, %r11
581523
jge _get_AAD_blocks\num_initial_blocks\operation
582524

583525
movdqu \XMM2, %xmm\i
526+
527+
/* read the last <16B of AAD */
528+
_get_AAD_rest\num_initial_blocks\operation:
584529
cmp $0, %r11
585530
je _get_AAD_done\num_initial_blocks\operation
586531

587-
pxor %xmm\i,%xmm\i
588-
589-
/* read the last <16B of AAD. since we have at least 4B of
590-
data right after the AAD (the ICV, and maybe some PT), we can
591-
read 4B/8B blocks safely, and then get rid of the extra stuff */
592-
_get_AAD_rest8\num_initial_blocks\operation:
593-
cmp $4, %r11
594-
jle _get_AAD_rest4\num_initial_blocks\operation
595-
movq (%r10), \TMP1
596-
add $8, %r10
597-
sub $8, %r11
598-
pslldq $8, \TMP1
599-
psrldq $8, %xmm\i
600-
pxor \TMP1, %xmm\i
601-
jmp _get_AAD_rest8\num_initial_blocks\operation
602-
_get_AAD_rest4\num_initial_blocks\operation:
603-
cmp $0, %r11
604-
jle _get_AAD_rest0\num_initial_blocks\operation
605-
mov (%r10), %eax
606-
movq %rax, \TMP1
607-
add $4, %r10
608-
sub $4, %r10
609-
pslldq $12, \TMP1
610-
psrldq $4, %xmm\i
611-
pxor \TMP1, %xmm\i
612-
_get_AAD_rest0\num_initial_blocks\operation:
613-
/* finalize: shift out the extra bytes we read, and align
614-
left. since pslldq can only shift by an immediate, we use
615-
vpshufb and an array of shuffle masks */
616-
movq %r12, %r11
617-
salq $4, %r11
618-
movdqu aad_shift_arr(%r11), \TMP1
619-
PSHUFB_XMM \TMP1, %xmm\i
620-
_get_AAD_rest_final\num_initial_blocks\operation:
532+
READ_PARTIAL_BLOCK %r10, %r11, \TMP1, %xmm\i
621533
PSHUFB_XMM %xmm14, %xmm\i # byte-reflect the AAD data
622534
pxor \XMM2, %xmm\i
623535
GHASH_MUL %xmm\i, \TMP3, \TMP1, \TMP2, \TMP4, \TMP5, \XMM1

0 commit comments

Comments
 (0)