android_kernel_samsung_msm8976/crypto
Eric Biggers 879a0046fe crypto: hmac - require that the underlying hash algorithm is unkeyed
commit af3ff8045bbf3e32f1a448542e73abb4c8ceb6f1 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>
2019-07-27 21:45:46 +02:00
..
asymmetric_keys crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks 2019-07-27 21:42:52 +02:00
async_tx raid6test: use prandom_bytes() 2013-04-29 18:28:42 -07:00
842.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
Kconfig arm: crypto: Add optimized SHA-256/224 2015-09-16 18:20:15 +05:30
Makefile crypto: improve gcc optimization flags for serpent and wp512 2019-07-27 21:43:58 +02:00
ablk_helper.c crypto: ablk_helper - Replace memcpy with struct assignment 2015-03-19 14:52:28 -07:00
ablkcipher.c crypto: skcipher - Add crypto_skcipher_has_setkey 2019-07-27 21:42:06 +02:00
aead.c
aes_generic.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
af_alg.c crypto: af_alg - Forbid bind(2) when nokey child sockets are present 2019-07-27 21:42:08 +02:00
ahash.c crypto: hash - Add crypto_ahash_has_setkey 2019-07-27 21:42:05 +02:00
algapi.c crypto: api - Clear CRYPTO_ALG_DEAD bit before registering an alg 2019-07-27 21:43:50 +02:00
algboss.c crypto: algboss - Hold ref count on larval 2013-06-25 19:15:17 +08:00
algif_hash.c crypto: algif_hash - avoid zero-sized array 2019-07-27 21:44:05 +02:00
algif_skcipher.c crypto: AF_ALG - remove SGL terminator indicator when chaining 2019-07-27 21:44:42 +02:00
ansi_cprng.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
anubis.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
api.c crypto: api - Only abort operations on fatal signal 2015-11-09 10:12:59 -08:00
arc4.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
authenc.c crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks 2019-07-27 21:42:52 +02:00
authencesn.c crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks 2019-07-27 21:42:52 +02:00
blkcipher.c crypto: skcipher - Fix blkcipher walk OOM crash 2019-07-27 21:42:09 +02:00
blowfish_common.c
blowfish_generic.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
camellia_generic.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
cast5_generic.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
cast6_generic.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
cast_common.c
cbc.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
ccm.c crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks 2019-07-27 21:42:52 +02:00
chainiv.c This is the 3.10.67 stable release 2015-04-24 18:04:40 -07:00
cipher.c
cmac.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
compress.c
crc32.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
crc32c.c crypto: crc32c - add missing crypto module alias 2015-02-11 14:48:18 +08:00
cryptd.c crypto: cryptd - Assign statesize properly 2019-07-27 21:44:03 +02:00
crypto_null.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
crypto_user.c crypto: user - lock crypto_alg_list on alg dump 2016-02-19 14:22:41 -08:00
crypto_wq.c crypto: crypto_wq - Fix late crypto work queue initialization 2014-06-07 13:25:35 -07:00
ctr.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
cts.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
deflate.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
des_generic.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
ecb.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
eseqiv.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
fcrypt.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
fips.c
gcm.c crypto: gcm - wait for crypto op not signal safe 2019-07-27 21:44:48 +02:00
gf128mul.c
ghash-generic.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
hmac.c crypto: hmac - require that the underlying hash algorithm is unkeyed 2019-07-27 21:45:46 +02:00
internal.h crypto: algboss - Hold ref count on larval 2013-06-25 19:15:17 +08:00
khazad.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
krng.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
lrw.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
lzo.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
md4.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
md5.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
memneq.c crypto: crypto_memneq - add equality testing of memory regions w/o timing leaks 2019-07-27 21:42:52 +02:00
michael_mic.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
pcbc.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
pcompress.c
pcrypt.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
proc.c
ripemd.h
rmd128.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
rmd160.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
rmd256.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
rmd320.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
rng.c
salsa20_generic.c crypto: salsa20 - fix blkcipher_walk API usage 2019-07-27 21:45:46 +02:00
scatterwalk.c crypto: scatterwalk - Fix test in scatterwalk_done 2019-07-27 21:41:53 +02:00
seed.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00
seqiv.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
serpent_generic.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
sha1_generic.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
sha256_generic.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
sha512_generic.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
shash.c crypto: hmac - require that the underlying hash algorithm is unkeyed 2019-07-27 21:45:46 +02:00
tcrypt.c crypto: tcrypt - add async cipher speed tests for blowfish 2013-04-25 21:09:03 +08:00
tcrypt.h
tea.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
testmgr.c crypto: camellia - add AVX2/AES-NI/x86_64 assembler implementation of camellia cipher 2013-04-25 21:09:07 +08:00
testmgr.h Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6 2013-05-02 14:53:12 -07:00
tgr192.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
twofish_common.c
twofish_generic.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
vmac.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
wp512.c crypto: add missing crypto module aliases 2015-01-29 17:40:57 -08:00
xcbc.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
xor.c
xts.c crypto: include crypto- module prefix in template 2015-01-29 17:40:57 -08:00
zlib.c crypto: prefix module autoloading with "crypto-" 2015-01-29 17:40:57 -08:00