linux/crypto
Eric Biggers 8f9c469348 crypto: authenc - fix parsing key with misaligned rta_len
Keys for "authenc" AEADs are formatted as an rtattr containing a 4-byte
'enckeylen', followed by an authentication key and an encryption key.
crypto_authenc_extractkeys() parses the key to find the inner keys.

However, it fails to consider the case where the rtattr's payload is
longer than 4 bytes but not 4-byte aligned, and where the key ends
before the next 4-byte aligned boundary.  In this case, 'keylen -=
RTA_ALIGN(rta->rta_len);' underflows to a value near UINT_MAX.  This
causes a buffer overread and crash during crypto_ahash_setkey().

Fix it by restricting the rtattr payload to the expected size.

Reproducer using AF_ALG:

	#include <linux/if_alg.h>
	#include <linux/rtnetlink.h>
	#include <sys/socket.h>

	int main()
	{
		int fd;
		struct sockaddr_alg addr = {
			.salg_type = "aead",
			.salg_name = "authenc(hmac(sha256),cbc(aes))",
		};
		struct {
			struct rtattr attr;
			__be32 enckeylen;
			char keys[1];
		} __attribute__((packed)) key = {
			.attr.rta_len = sizeof(key),
			.attr.rta_type = 1 /* CRYPTO_AUTHENC_KEYA_PARAM */,
		};

		fd = socket(AF_ALG, SOCK_SEQPACKET, 0);
		bind(fd, (void *)&addr, sizeof(addr));
		setsockopt(fd, SOL_ALG, ALG_SET_KEY, &key, sizeof(key));
	}

It caused:

	BUG: unable to handle kernel paging request at ffff88007ffdc000
	PGD 2e01067 P4D 2e01067 PUD 2e04067 PMD 2e05067 PTE 0
	Oops: 0000 [#1] SMP
	CPU: 0 PID: 883 Comm: authenc Not tainted 4.20.0-rc1-00108-g00c9fe37a7f27 #13
	Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
	RIP: 0010:sha256_ni_transform+0xb3/0x330 arch/x86/crypto/sha256_ni_asm.S:155
	[...]
	Call Trace:
	 sha256_ni_finup+0x10/0x20 arch/x86/crypto/sha256_ssse3_glue.c:321
	 crypto_shash_finup+0x1a/0x30 crypto/shash.c:178
	 shash_digest_unaligned+0x45/0x60 crypto/shash.c:186
	 crypto_shash_digest+0x24/0x40 crypto/shash.c:202
	 hmac_setkey+0x135/0x1e0 crypto/hmac.c:66
	 crypto_shash_setkey+0x2b/0xb0 crypto/shash.c:66
	 shash_async_setkey+0x10/0x20 crypto/shash.c:223
	 crypto_ahash_setkey+0x2d/0xa0 crypto/ahash.c:202
	 crypto_authenc_setkey+0x68/0x100 crypto/authenc.c:96
	 crypto_aead_setkey+0x2a/0xc0 crypto/aead.c:62
	 aead_setkey+0xc/0x10 crypto/algif_aead.c:526
	 alg_setkey crypto/af_alg.c:223 [inline]
	 alg_setsockopt+0xfe/0x130 crypto/af_alg.c:256
	 __sys_setsockopt+0x6d/0xd0 net/socket.c:1902
	 __do_sys_setsockopt net/socket.c:1913 [inline]
	 __se_sys_setsockopt net/socket.c:1910 [inline]
	 __x64_sys_setsockopt+0x1f/0x30 net/socket.c:1910
	 do_syscall_64+0x4a/0x180 arch/x86/entry/common.c:290
	 entry_SYSCALL_64_after_hwframe+0x49/0xbe

Fixes: e236d4a89a ("[CRYPTO] authenc: Move enckeylen into key itself")
Cc: <stable@vger.kernel.org> # v2.6.25+
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-01-10 21:37:31 +08:00
..
asymmetric_keys KEYS: asym_tpm: Add support for the sign operation [ver #2] 2018-10-26 09:30:47 +01:00
async_tx async_pq: Remove VLA usage 2018-06-18 20:17:38 +05:30
842.c
Kconfig Kconfig updates for v4.21 2018-12-29 13:03:29 -08:00
Makefile crypto: user - made crypto_user_stat optional 2018-12-07 14:15:00 +08:00
ablkcipher.c crypto: skcipher - remove remnants of internal IV generators 2018-12-23 11:52:45 +08:00
acompress.c crypto: user - clean up report structure copying 2018-11-09 17:41:39 +08:00
adiantum.c crypto: adiantum - fix leaking reference to hash algorithm 2018-12-13 18:24:59 +08:00
aead.c crypto: user - clean up report structure copying 2018-11-09 17:41:39 +08:00
aegis.h crypto: aegis/generic - fix for big endian systems 2018-10-08 13:44:53 +08:00
aegis128.c crypto: aead - remove useless setting of type flags 2018-07-09 00:30:26 +08:00
aegis128l.c crypto: aead - remove useless setting of type flags 2018-07-09 00:30:26 +08:00
aegis256.c crypto: aead - remove useless setting of type flags 2018-07-09 00:30:26 +08:00
aes_generic.c crypto: arm/aes - add some hardening against cache-timing attacks 2018-11-09 17:36:48 +08:00
aes_ti.c crypto: aes_ti - disable interrupts while accessing S-box 2018-11-09 17:36:48 +08:00
af_alg.c Revert "net: simplify sock_poll_wait" 2018-10-23 10:57:06 -07:00
ahash.c crypto: user - fix use_after_free of struct xxx_request 2018-12-07 14:15:00 +08:00
akcipher.c crypto: user - clean up report structure copying 2018-11-09 17:41:39 +08:00
algapi.c crypto: user - Add crypto_stats_init 2018-12-07 14:15:00 +08:00
algboss.c crypto: api - Introduce notifier for new crypto algorithms 2018-09-04 11:37:04 +08:00
algif_aead.c crypto: null - Remove VLA usage of skcipher 2018-09-28 12:46:08 +08:00
algif_hash.c crypto: hash - Remove VLA usage 2018-09-04 11:35:03 +08:00
algif_rng.c net: remove sock_no_poll 2018-05-26 09:16:44 +02:00
algif_skcipher.c Revert changes to convert to ->poll_mask() and aio IOCB_CMD_POLL 2018-06-28 10:40:47 -07:00
ansi_cprng.c
anubis.c
api.c evm: Don't deadlock if a crypto algorithm is unavailable 2018-07-18 07:27:22 -04:00
arc4.c
authenc.c crypto: authenc - fix parsing key with misaligned rta_len 2019-01-10 21:37:31 +08:00
authencesn.c crypto: null - Remove VLA usage of skcipher 2018-09-28 12:46:08 +08:00
blkcipher.c crypto: skcipher - remove remnants of internal IV generators 2018-12-23 11:52:45 +08:00
blowfish_common.c
blowfish_generic.c
camellia_generic.c
cast5_generic.c
cast6_generic.c
cast_common.c
cbc.c crypto: do not free algorithm before using 2018-11-29 14:53:59 +08:00
ccm.c crypto: ccm - Remove VLA usage 2018-09-04 11:35:03 +08:00
cfb.c Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6 2018-12-07 13:59:10 +08:00
chacha20poly1305.c crypto: chacha20-generic - refactor to allow varying number of rounds 2018-11-20 14:26:55 +08:00
chacha_generic.c crypto: chacha - add XChaCha12 support 2018-11-20 14:26:55 +08:00
cipher.c
cmac.c
compress.c
crc32_generic.c crypto: crc32-generic - remove __crc32_le() 2018-05-27 00:12:09 +08:00
crc32c_generic.c crypto: crc32c-generic - remove cra_alignmask 2018-05-27 00:12:08 +08:00
crct10dif_common.c
crct10dif_generic.c
cryptd.c crypto: skcipher - remove remnants of internal IV generators 2018-12-23 11:52:45 +08:00
crypto_engine.c
crypto_null.c crypto: null - Remove VLA usage of skcipher 2018-09-28 12:46:08 +08:00
crypto_user_base.c crypto: user - remove unused dump functions 2018-12-23 11:52:44 +08:00
crypto_user_stat.c crypto: user - remove unused dump functions 2018-12-23 11:52:44 +08:00
crypto_wq.c
ctr.c crypto: skcipher - remove remnants of internal IV generators 2018-12-23 11:52:45 +08:00
cts.c
deflate.c
des_generic.c
dh.c crypto: dh - fix memory leak 2018-07-20 13:51:21 +08:00
dh_helper.c crypto: dh - make crypto_dh_encode_key() make robust 2018-08-03 18:06:06 +08:00
drbg.c crypto: drbg - in-place cipher operation for CTR 2018-08-03 18:05:48 +08:00
ecb.c
ecc.c crypto: ecc - regularize scalar for scalar multiplication 2018-11-16 14:11:04 +08:00
ecc.h
ecc_curve_defs.h crypto: ecdh - fix typo of P-192 b value 2018-07-20 13:51:22 +08:00
ecdh.c
ecdh_helper.c
echainiv.c crypto: null - Remove VLA usage of skcipher 2018-09-28 12:46:08 +08:00
fcrypt.c
fips.c
gcm.c crypto: null - Remove VLA usage of skcipher 2018-09-28 12:46:08 +08:00
gf128mul.c
ghash-generic.c crypto: shash - remove useless setting of type flags 2018-07-09 00:30:24 +08:00
hash_info.c crypto: streebog - register Streebog in hash info for IMA 2018-11-16 14:09:40 +08:00
hmac.c
internal.h crypto: api - Introduce notifier for new crypto algorithms 2018-09-04 11:37:04 +08:00
jitterentropy-kcapi.c
jitterentropy.c
keywrap.c
khazad.c
kpp.c crypto: user - clean up report structure copying 2018-11-09 17:41:39 +08:00
lrw.c crypto: lrw - fix rebase error after out of bounds fix 2018-10-05 10:22:48 +08:00
lz4.c crypto: remove useless initializations of cra_list 2018-11-20 14:26:55 +08:00
lz4hc.c crypto: remove useless initializations of cra_list 2018-11-20 14:26:55 +08:00
lzo.c
md4.c crypto: shash - remove useless setting of type flags 2018-07-09 00:30:24 +08:00
md5.c crypto: shash - remove useless setting of type flags 2018-07-09 00:30:24 +08:00
memneq.c
michael_mic.c
morus640.c crypto: morus/generic - fix for big endian systems 2018-10-08 13:44:53 +08:00
morus1280.c crypto: morus/generic - fix for big endian systems 2018-10-08 13:44:53 +08:00
nhpoly1305.c crypto: adiantum - adjust some comments to match latest paper 2018-12-13 18:24:59 +08:00
ofb.c crypto: ofb - add output feedback mode 2018-09-28 12:46:26 +08:00
pcbc.c crypto: do not free algorithm before using 2018-11-29 14:53:59 +08:00
pcrypt.c Merge branch 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6 2018-12-27 13:53:32 -08:00
poly1305_generic.c crypto: poly1305 - add Poly1305 core API 2018-11-20 14:26:56 +08:00
proc.c proc: introduce proc_create_seq{,_data} 2018-05-16 07:23:35 +02:00
ripemd.h
rmd128.c crypto: shash - remove useless setting of type flags 2018-07-09 00:30:24 +08:00
rmd160.c crypto: shash - remove useless setting of type flags 2018-07-09 00:30:24 +08:00
rmd256.c crypto: rmd256 - use swap macro in rmd256_transform 2018-07-27 19:28:36 +08:00
rmd320.c crypto: rmd320 - use swap macro in rmd320_transform 2018-07-27 19:28:36 +08:00
rng.c crypto: user - fix use_after_free of struct xxx_request 2018-12-07 14:15:00 +08:00
rsa-pkcs1pad.c crypto: rsa-pkcs1pad: Allow hash to be optional [ver #2] 2018-10-26 09:30:46 +01:00
rsa.c
rsa_helper.c
rsaprivkey.asn1
rsapubkey.asn1
salsa20_generic.c crypto: salsa20-generic - don't unnecessarily use atomic walk 2018-12-23 11:52:44 +08:00
scatterwalk.c crypto: scatterwalk - remove 'chain' argument from scatterwalk_crypto_chain() 2018-08-03 18:06:03 +08:00
scompress.c crypto: user - clean up report structure copying 2018-11-09 17:41:39 +08:00
seed.c
seqiv.c crypto: null - Remove VLA usage of skcipher 2018-09-28 12:46:08 +08:00
serpent_generic.c
sha1_generic.c crypto: shash - remove useless setting of type flags 2018-07-09 00:30:24 +08:00
sha3_generic.c Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux 2018-08-03 17:55:12 +08:00
sha256_generic.c crypto: shash - remove useless setting of type flags 2018-07-09 00:30:24 +08:00
sha512_generic.c crypto: shash - remove useless setting of type flags 2018-07-09 00:30:24 +08:00
shash.c crypto: user - clean up report structure copying 2018-11-09 17:41:39 +08:00
simd.c crypto: simd - correctly take reqsize of wrapped skcipher into account 2018-11-09 17:35:43 +08:00
skcipher.c crypto: skcipher - remove remnants of internal IV generators 2018-12-23 11:52:45 +08:00
sm3_generic.c crypto: shash - remove useless setting of type flags 2018-07-09 00:30:24 +08:00
sm4_generic.c
streebog_generic.c crypto: streebog - add Streebog hash function 2018-11-16 14:09:40 +08:00
tcrypt.c crypto: tcrypt - add block size of 1472 to skcipher template 2018-12-13 18:24:40 +08:00
tcrypt.h crypto: testmgr - update sm4 test vectors 2018-09-28 12:46:26 +08:00
tea.c
testmgr.c crypto: adiantum - add Adiantum support 2018-11-20 14:26:56 +08:00
testmgr.h crypto: xchacha20 - fix comments for test vectors 2018-12-13 18:24:59 +08:00
tgr192.c crypto: shash - remove useless setting of type flags 2018-07-09 00:30:24 +08:00
twofish_common.c
twofish_generic.c
vmac.c crypto: vmac - remove insecure version with hardcoded nonce 2018-07-01 21:00:44 +08:00
wp512.c crypto: shash - remove useless setting of type flags 2018-07-09 00:30:24 +08:00
xcbc.c crypto: xcbc - Remove VLA usage 2018-09-04 11:35:03 +08:00
xor.c
xts.c crypto: xts - Drop use of auxiliary buffer 2018-09-21 13:24:50 +08:00
zstd.c