linux/crypto
Eric Biggers 150fbc1221 crypto: hmac - require that the underlying hash algorithm is unkeyed
commit af3ff8045b upstream.

Because the HMAC template didn't check that its underlying hash
algorithm is unkeyed, trying to use "hmac(hmac(sha3-512-generic))"
through AF_ALG or through KEYCTL_DH_COMPUTE resulted in the inner HMAC
being used without having been keyed, resulting in sha3_update() being
called without sha3_init(), causing a stack buffer overflow.

This is a very old bug, but it seems to have only started causing real
problems when SHA-3 support was added (requires CONFIG_CRYPTO_SHA3)
because the innermost hash's state is ->import()ed from a zeroed buffer,
and it just so happens that other hash algorithms are fine with that,
but SHA-3 is not.  However, there could be arch or hardware-dependent
hash algorithms also affected; I couldn't test everything.

Fix the bug by introducing a function crypto_shash_alg_has_setkey()
which tests whether a shash algorithm is keyed.  Then update the HMAC
template to require that its underlying hash algorithm is unkeyed.

Here is a reproducer:

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

    int main()
    {
        int algfd;
        struct sockaddr_alg addr = {
            .salg_type = "hash",
            .salg_name = "hmac(hmac(sha3-512-generic))",
        };
        char key[4096] = { 0 };

        algfd = socket(AF_ALG, SOCK_SEQPACKET, 0);
        bind(algfd, (const struct sockaddr *)&addr, sizeof(addr));
        setsockopt(algfd, SOL_ALG, ALG_SET_KEY, key, sizeof(key));
    }

Here was the KASAN report from syzbot:

    BUG: KASAN: stack-out-of-bounds in memcpy include/linux/string.h:341  [inline]
    BUG: KASAN: stack-out-of-bounds in sha3_update+0xdf/0x2e0  crypto/sha3_generic.c:161
    Write of size 4096 at addr ffff8801cca07c40 by task syzkaller076574/3044

    CPU: 1 PID: 3044 Comm: syzkaller076574 Not tainted 4.14.0-mm1+ #25
    Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS  Google 01/01/2011
    Call Trace:
      __dump_stack lib/dump_stack.c:17 [inline]
      dump_stack+0x194/0x257 lib/dump_stack.c:53
      print_address_description+0x73/0x250 mm/kasan/report.c:252
      kasan_report_error mm/kasan/report.c:351 [inline]
      kasan_report+0x25b/0x340 mm/kasan/report.c:409
      check_memory_region_inline mm/kasan/kasan.c:260 [inline]
      check_memory_region+0x137/0x190 mm/kasan/kasan.c:267
      memcpy+0x37/0x50 mm/kasan/kasan.c:303
      memcpy include/linux/string.h:341 [inline]
      sha3_update+0xdf/0x2e0 crypto/sha3_generic.c:161
      crypto_shash_update+0xcb/0x220 crypto/shash.c:109
      shash_finup_unaligned+0x2a/0x60 crypto/shash.c:151
      crypto_shash_finup+0xc4/0x120 crypto/shash.c:165
      hmac_finup+0x182/0x330 crypto/hmac.c:152
      crypto_shash_finup+0xc4/0x120 crypto/shash.c:165
      shash_digest_unaligned+0x9e/0xd0 crypto/shash.c:172
      crypto_shash_digest+0xc4/0x120 crypto/shash.c:186
      hmac_setkey+0x36a/0x690 crypto/hmac.c:66
      crypto_shash_setkey+0xad/0x190 crypto/shash.c:64
      shash_async_setkey+0x47/0x60 crypto/shash.c:207
      crypto_ahash_setkey+0xaf/0x180 crypto/ahash.c:200
      hash_setkey+0x40/0x90 crypto/algif_hash.c:446
      alg_setkey crypto/af_alg.c:221 [inline]
      alg_setsockopt+0x2a1/0x350 crypto/af_alg.c:254
      SYSC_setsockopt net/socket.c:1851 [inline]
      SyS_setsockopt+0x189/0x360 net/socket.c:1830
      entry_SYSCALL_64_fastpath+0x1f/0x96

Reported-by: syzbot <syzkaller@googlegroups.com>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
History 2018-01-01 20:52:12 +00:00
..
asymmetric_keys crypto: public_key: select CRYPTO_AKCIPHER 2016-08-22 22:38:02 +01:00
async_tx async_pq_val: fix DMA memory leak 2017-02-23 03:54:09 +00:00
842.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
ablk_helper.c crypto: ablk_helper - Replace memcpy with struct assignment 2013-10-07 14:16:57 +08:00
ablkcipher.c crypto: {blk,giv}cipher: Set has_setkey 2016-03-08 12:15:18 +00:00
aead.c crypto: user - fix info leaks in report API 2013-02-19 20:27:03 +08:00
aes_generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
af_alg.c crypto: af_alg - Forbid bind(2) when nokey child sockets are present 2016-02-02 19:09:57 +00:00
ahash.c crypto: ahash - Fix EINPROGRESS notification callback 2017-06-05 21:17:22 +01:00
algapi.c crypto: api - Clear CRYPTO_ALG_DEAD bit before registering an alg 2017-03-16 02:27:04 +00:00
algboss.c crypto: algboss - Hold ref count on larval 2013-06-25 19:15:17 +08:00
algif_hash.c crypto: algif_hash - wait for crypto_ahash_init() to complete 2016-02-25 00:18:38 +00:00
algif_skcipher.c crypto: algif_skcipher - Fix race condition in skcipher_check_key 2016-02-24 14:41:18 +00:00
ansi_cprng.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
anubis.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
api.c crypto: api - Only abort operations on fatal signal 2015-11-16 11:27:09 +00:00
arc4.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
authenc.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
authencesn.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
blkcipher.c crypto: skcipher - Fix blkcipher walk OOM crash 2016-11-20 01:17:26 +00:00
blowfish_common.c crypto: blowfish - split generic and common c code 2011-09-22 21:25:25 +10:00
blowfish_generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
camellia_generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
cast5_generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
cast6_generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
cast_common.c crypto: make tables used from assembler __visible 2013-08-14 20:42:03 +10:00
cbc.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
ccm.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
chainiv.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
cipher.c crypto: cipher - Fix checkpatch errors 2010-02-16 20:31:37 +08:00
cmac.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
compress.c crypto: compress - Fix checkpatch errors 2010-02-16 20:31:04 +08:00
crc32.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
crc32c_generic.c crypto: crc32c - Fix crc32c soft dependency 2016-02-02 19:10:02 +00:00
crct10dif_common.c crypto: crct10dif - Add fallback for broken initrds 2013-09-12 15:31:34 +10:00
crct10dif_generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
cryptd.c crypto: cryptd - initialize child shash_desc on import 2016-11-20 01:17:24 +00:00
crypto_null.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
crypto_user.c crypto: user - lock crypto_alg_list on alg dump 2016-02-25 10:34:46 +00:00
crypto_wq.c crypto: crypto_wq - Fix late crypto work queue initialization 2014-03-21 21:54:28 +08:00
ctr.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
cts.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
deflate.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
des_generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
ecb.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
eseqiv.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
fcrypt.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
fips.c crypto: api - Add fips_enable flag 2008-08-29 15:50:02 +10:00
gcm.c crypto: gcm - wait for crypto op not signal safe 2017-09-15 18:29:48 +01:00
gf128mul.c crypto: gf128mul - fix call to memset() 2011-07-08 17:21:21 +08:00
ghash-generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
hash_info.c crypto: provide single place for hash algo information 2013-10-25 17:14:03 -04:00
hmac.c crypto: hmac - require that the underlying hash algorithm is unkeyed 2018-01-01 20:52:12 +00:00
internal.h crypto: algboss - Hold ref count on larval 2013-06-25 19:15:17 +08:00
Kconfig crypto: sha - SHA1 transform x86_64 AVX2 2014-03-21 21:54:30 +08:00
khazad.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
krng.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
lrw.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
lz4.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
lz4hc.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
lzo.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
Makefile crypto: improve gcc optimization flags for serpent and wp512 2017-04-04 22:21:55 +01:00
md4.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
md5.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
memneq.c crypto: memneq - fix for archs without efficient unaligned access 2013-12-09 20:09:12 +08:00
michael_mic.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
pcbc.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
pcompress.c crypto: user - fix info leaks in report API 2013-02-19 20:27:03 +08:00
pcrypt.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
proc.c crypto: add module.h to those files that are explicitly using it 2011-10-31 19:31:11 -04:00
ripemd.h [CRYPTO] ripemd: Put all common RIPEMD values in header file 2008-07-10 20:35:12 +08:00
rmd128.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
rmd160.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
rmd256.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
rmd320.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
rng.c crypto: user - fix info leaks in report API 2013-02-19 20:27:03 +08:00
salsa20_generic.c crypto: salsa20 - fix blkcipher_walk API usage 2018-01-01 20:52:11 +00:00
scatterwalk.c crypto: scatterwalk - Fix test in scatterwalk_done 2016-11-20 01:16:53 +00:00
seed.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00
seqiv.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
serpent_generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
sha1_generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
sha256_generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
sha512_generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
shash.c crypto: hmac - require that the underlying hash algorithm is unkeyed 2018-01-01 20:52:12 +00:00
tcrypt.c crypto: testmgr - add aead cbc des, des3_ede tests 2014-05-22 21:03:18 +08:00
tcrypt.h crypto: tcrypt - Added speed tests for AEAD crypto alogrithms in tcrypt test suite 2013-12-20 20:06:25 +08:00
tea.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
testmgr.c crypto: testmgr - add aead cbc des, des3_ede tests 2014-05-22 21:03:18 +08:00
testmgr.h crypto: testmgr - add aead cbc des, des3_ede tests 2014-05-22 21:03:18 +08:00
tgr192.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
twofish_common.c crypto: twofish-x86_64-3way - add lrw support 2011-11-09 11:53:32 +08:00
twofish_generic.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
vmac.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
wp512.c crypto: add missing crypto module aliases 2015-02-04 10:57:50 +00:00
xcbc.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
xor.c add further __init annotations to crypto/xor.c 2012-10-11 13:42:32 +11:00
xts.c crypto: include crypto- module prefix in template 2015-02-04 10:57:49 +00:00
zlib.c crypto: prefix module autoloading with "crypto-" 2015-02-04 10:57:48 +00:00